Self-executing For JS

Question:

In javascript, when would you want to use this:

(function(){ //Bunch of code... })();

Answer:

Its all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of javascript code.


因为js他是从上到下解释型语言
一旦你有几十个js
用了self executiing就保证你所有的变量都是在当前这个self execution里面
不然你 var ctrl = ..........
每个js里面都有var ctrl,到时候就覆盖了

你可能感兴趣的:(Self-executing For JS)