开发者

What (function(){})(); mean? [duplicate]

开发者 https://www.devze.com 2023-02-14 23:20 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What does this “(function(){});”, a fu开发者_开发百科nction inside brackets, mean in javascript ?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What does this “(function(){});”, a fu开发者_开发百科nction inside brackets, mean in javascript ?

(function(){
    ---this code at here ----
})();

What does (function(){})(); mean? Please explain it to me.


It makes an anonymous function and executes it. You use it to prevent variables from poluting the global scope.

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

alert(test); //test will be undefined here


The function is immediately executed after parsing it.


Well, you use a function expression as a closure which gets executed immediately and 'this code at here' will not pollute global namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消