请用 JavaScript 解释一下这段代码
我在很多脚本中看到这种模式
(function(){})();
它是什么以及为什么使用它?
I see in a lot of scripts this pattern
(function(){})();
What is it and why use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它用于强制创建本地范围,避免声明污染当前(通常是全局)范围。
如果您想避免使用匿名函数,可以这样重写:
但是匿名函数语法的优点是父作用域或全局作用域甚至不会被函数名称污染。
这也是在 JavaScript 中实现信息隐藏的好方法,因为此范围内的声明(函数和变量)从外部不可见。但他们仍然可以看到对方,并且当 JavaScript 实现闭包时,在这样的作用域内声明的函数将可以访问同一作用域内的其他声明。
It's used to force the creation of a local scope it avoid poluting the current (often global) scope with the declarations.
It could be rewritten like this if you want to avoid the anonymous function :
But the anonymous function syntax have the advantage that the parent or global scope isn't even poluted by the name of the function.
It's also a good way to implement information hiding in javascript as declarations (functions and variables) in this scope won't be visible from the outside. But they could still see each other and as javascript implement closures functions declared inside such a scope will have access to other declarations in the same scope.
那就是定义一个没有名称的函数,并立即调用它。因为 Javascript 函数充当闭包(持久作用域),所以这是创建一组互连对象或函数的有用方法。
That is defining a function with no name, and immediately calling it. Because Javascript functions act as closures -- a persistent scope -- this is a useful way to create a set of interconnected objects or functions.
匿名函数是在不绑定到标识符的情况下定义并可能调用的函数(或子例程)。
An anonymous function is a function (or a subroutine) defined, and possibly called, without being bound to an identifier.
这是创建闭包的基本语法。更典型的是,它会包含一些代码:
这相当于
这样做的最大原因是清洁;在任何函数外部声明的任何变量都是全局的;但是,在此函数内部声明的变量包含在该函数内部,不会影响该函数外部的任何代码或与该函数外部的任何代码交互。将任何类型的可重用插件包装在闭包中是一个很好的做法。
This is the basic syntax for creating a closure. More typically, it'd contain some code:
This is equivalent to
The biggest reason for doing this is cleanliness; any variables declared outside of any function are global; however, variables declared inside of this function are contained inside of this function and won't affect or interactive with any code outside of the function. It's good practice to wrap any kind of reusable plugin in a closure.
它立即执行匿名函数。
它基本上与:
但不需要使用附加变量。
您需要将其括在额外的括号中才能将函数作为表达式的结果 - 否则它会被理解为函数声明,并且您无法执行声明。
它主要用于作用域保护 - 因为 JS 具有函数作用域,因此在此类函数内定义为 var x; 的每个变量都将保留在其函数局部作用域中。
所有这些只是意味着“立即执行该函数内的所有内容,而不污染全局范围”。
它也常用于众所周知的模式,例如模块模式和显示模块模式。请参阅http://www.adequatelygood.com/2010/3 /JavaScript-Module-Pattern-In-Depth 了解更多详细信息。
It's immediately executing anonymous function.
It's basically the same as:
but does not require usage of additional variable.
you need to wrap it in additional parenthesis to get the function as a result of your expression - otherwise it's understood as function declaration, and you can't execute a declaration.
it's mostly used for scope protection - because JS has functional scope, every variable defined as
var x;
inside such function will be kept in it's function local scope.all of this simply means 'immediately execute everything inside this function without polluting the global scope'.
it's also commonly used in well known patterns, such as module pattern and revealing module pattern. please see http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth for more detail.
它称为立即函数或匿名闭包,是模块模式。
它用于为代码创建私有本地范围。
It is called an immediate function or an anonymous closure and is the basis of the module pattern.
It is used to create a private local scope for code.