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,到时候就覆盖了
本文探讨了在JavaScript中使用自执行函数的作用,重点强调了变量作用域的重要性,通过实例说明了如何确保代码中变量的安全性和独立性。
1410

被折叠的 条评论
为什么被折叠?



