【发布时间】:2011-07-08 17:06:06
【问题描述】:
可能重复:
What does this “(function(){});”, a function inside brackets, mean in javascript ?
(function(){
---this code at here ----
})();
(function(){})(); 是什么意思?请给我解释一下。
【问题讨论】:
标签: javascript
可能重复:
What does this “(function(){});”, a function inside brackets, mean in javascript ?
(function(){
---this code at here ----
})();
(function(){})(); 是什么意思?请给我解释一下。
【问题讨论】:
标签: javascript
好吧,你使用一个函数表达式作为一个立即执行的闭包,“这里的代码”不会污染全局命名空间。
【讨论】:
它创建一个匿名函数并执行它。您可以使用它来防止变量污染全局范围。
(function(){
var test = "Hello";
})();
alert(test); //test will be undefined here
【讨论】:
函数解析后立即执行。
【讨论】: