可能重复:
What does this “(function(){});”, a function inside brackets, mean in javascript ?
(function(){
---this code at here ----
})();
(function(){})();
是什么意思?请解释一下。
答案 0 :(得分:5)
它创建一个匿名函数并执行它。您可以使用它来防止变量违反全局范围。
(function(){
var test = "Hello";
})();
alert(test); //test will be undefined here
答案 1 :(得分:4)
解析后立即执行该功能。
答案 2 :(得分:0)
好吧,你使用一个函数表达式作为一个闭包,立即执行,“这里的代码”不会污染全局命名空间。