javascript 为什么要使用自执行功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17058606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why using self executing function?
提问by
In many places I see scripts like this:
在很多地方我看到这样的脚本:
(function () {
var hello = 'Hello World';
alert(hello);
})();
Why not just write it like this, without any function:
为什么不直接这样写,没有任何功能:
var hello = 'Hello World';
alert(hello);
回答by
We use self executing function, to manage Variable Scope.
我们使用自执行函数来管理变量作用域。
The scope of a variable is the region of your program in which it is defined. A global variable has global scope; it is defined everywhere in your JavaScript code. (Even in your functions). On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.
变量的作用域是定义它的程序区域。全局变量具有全局作用域;它在您的 JavaScript 代码中的任何地方都有定义。(即使在您的功能中)。另一方面,在函数内声明的变量仅在函数体内定义。它们是局部变量并具有局部作用域。函数参数也算作局部变量,并且只在函数体内定义。
var scope = "global";
function checkscope() {
alert(scope);
}
checkscope(); // global
As you see, you can access the scope
variable inside your function,
but, within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.
如您所见,您可以访问scope
函数内部的变量,但是,在函数体内,局部变量优先于同名的全局变量。如果你声明一个与全局变量同名的局部变量或函数参数,你实际上隐藏了全局变量。
var scope = "global";
function checkscope() {
var scope = "local";
alert(scope);
}
checkscope(); // local
alert(scope); // global
As you see, variable inside the function, won't overwrite global variables. Because of this feature, we put the code inside the self executing function, to prevent overwriting the other variables, when our code get big and big.
如您所见,函数内部的变量不会覆盖全局变量。由于这个特性,我们将代码放在自执行函数中,以防止在我们的代码变得越来越大时覆盖其他变量。
// thousand line of codes
// written a year ago
// now you want to add some peice of code
// and you don't know what you have done in the past
// just put the new code in the self executing function
// and don't worry about your variable names
(function () {
var i = 'I';
var can = 'CAN';
var define = 'DEFINE';
var variables = 'VARIABLES';
var without = 'WITHOUT';
var worries = 'WORRIES';
var statement = [i, can, define, variables, without, worries];
alert(statement.join(' '));
// I CAN DEFINE VARIABLES WITHOUT WORRIES
}());
- You can read more about JavaScript on this book: JavaScript: The Definitive Guide, 6th Edition.
- 您可以在本书中阅读有关 JavaScript 的更多信息:JavaScript:权威指南,第 6 版。
回答by Ted Hopp
回答by Robin Maben
Apart from keeping the global namespace clean, they are also useful for establishing private methods for accessible functions while still exposing some properties for later use -
除了保持全局命名空间干净外,它们还有助于为可访问的函数建立私有方法,同时仍然公开一些属性供以后使用 -
var counter = (function(){
var i = 0;
return {
get: function(){
return i;
},
set: function( val ){
i = val;
},
increment: function() {
return ++i;
}
};
}());
// 'counter' is an object with properties, which in this case happen to be
// methods.
counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5
回答by Maxim Krizhanovsky
There can be a couple of reasons.
可能有几个原因。
The first one is protecting the scope, as the function creates a new scope.
第一个是保护作用域,因为函数会创建一个新的作用域。
Another one can be binding variables, for example
另一个可以是绑定变量,例如
for (var i = 0; i < n; i++) {
element.onclick = (function(i){
return function(){
// do something with i
}
})(i);
}