什么(function(){})(); 意思?

meo*_*hia 2 javascript

可能重复:
这个"(function(){});",括号内的函数,在javascript中是什么意思?

(function(){
    ---this code at here ----
})();
Run Code Online (Sandbox Code Playgroud)

什么(function(){})();意思?请向我解释一下.

Dec*_*cko 5

它创建一个匿名函数并执行它.您可以使用它来防止变量违反全局范围.

(function(){
  var test = "Hello";
})();

alert(test); //test will be undefined here
Run Code Online (Sandbox Code Playgroud)