开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜