为什么在这个函数定义中使用'window.angular'就像这样? [英] Why is 'window.angular' used like so, in this function definition?

查看:497
本文介绍了为什么在这个函数定义中使用'window.angular'就像这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解一个我需要用来与Django集成的angularjs文件,它有一个我不熟悉的奇怪语法(记住我是初级开发人员,所以这可能是你的面包和黄油)...

它就像这样:

  (函数(角度,未定义){
'使用脚本';
var djng_forms_module = angular.module('ng.django.forms',[]);
函数hasCode(s){
return .....
}
var foo = .....
}(window.angular));

我见过这个关于窗口的javascript概念,它显示窗口部分是顶级(?)级别对象浏览器在加载页面时创建。



运行 console.log(window.angular)打印出一个负载内部有角的东西。所以我猜这就是AngularJS的内部特性......?

但是,为什么这个奇怪的封装作为一个函数(与JavaScript有关的一个'功能性'语言)?



完整的脚本是这里,我不明白为什么它在这个函数定义中使用 window.angular (而不是正常的做事方式)。看起来这样设置意味着当我通过 script 标签导入它时,它不适用于我的应用程序。

解决方案 window.angular 是一个全局的angularjs变量,它是在脚本标记完全加载完成后创建的。您粘贴的代码片段确保在此变量填充后执行它。它可能以这种冗长的方式编写的一个原因就是它的自动生成特性。在更广的范围内,它可能会影响脚本的执行顺序或使用不同版本的angularjs库。


I'm trying to understand an angularjs file I need to use to integrate with Django, and it has a weird syntax I'm not familiar with (bear in mind I'm a junior dev, so this may be your bread and butter)...

It goes something like:

(function(angular, undefined){
    'use script';
    var djng_forms_module = angular.module('ng.django.forms', []);
    funtion hasCode(s){
        return .....
    }
    var foo = .....
}(window.angular));

I've seen this about the javascript concept of a window and it shows that the window part is the top (?) level object the browser creates when it loads the page.

Running console.log(window.angular) prints out a load of internal angular stuff. So I'm guessing that is the internal guts of AngularJS...?

But why this weird encapsulation as a function (something to do with JavaScript being a 'functional' language)?

The full script is here and I can't figure out why it uses window.angular in this function definition (as opposed to the normal way of doing things). It seems like this set up means it's not working for my application when I import it via script tags.

解决方案

window.angular is the global angularjs variable which is created once angularjs has been fully loaded from a script tag. The code fragment you have pasted ensures that it is executed after the population of this variable. One reason it might be written in that verbose way is simply its auto-generated nature. In a wider context, it may have implication to the order in which scripts are executed or to using different versions of the angularjs library.

这篇关于为什么在这个函数定义中使用'window.angular'就像这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆