请用 JavaScript 解释一下这段代码

发布于 2024-10-27 11:09:28 字数 86 浏览 4 评论 0原文

我在很多脚本中看到这种模式

(function(){})();

它是什么以及为什么使用它?

I see in a lot of scripts this pattern

(function(){})();

What is it and why use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌面超妹 2024-11-03 11:09:28

它用于强制创建本地范围,避免声明污染当前(通常是全局)范围。

如果您想避免使用匿名函数,可以这样重写:

var scope = function() { /*...*/ };
scope();

但是匿名函数语法的优点是父作用域或全局作用域甚至不会被函数名称污染。

(function() { /*...*/ })();

这也是在 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 :

var scope = function() { /*...*/ };
scope();

But the anonymous function syntax have the advantage that the parent or global scope isn't even poluted by the name of the function.

(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.

以酷 2024-11-03 11:09:28

那就是定义一个没有名称的函数,并立即调用它。因为 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.

给我一枪 2024-11-03 11:09:28

匿名函数是在不绑定到标识符的情况下定义并可能调用的函数(或子例程)。

An anonymous function is a function (or a subroutine) defined, and possibly called, without being bound to an identifier.

土豪我们做朋友吧 2024-11-03 11:09:28

这是创建闭包的基本语法。更典型的是,它会包含一些代码:

(function(){
    //Your Code Here
})();

这相当于

var some_function = function() {
    //Your Code Here
};
some_function();

这样做的最大原因是清洁;在任何函数外部声明的任何变量都是全局的;但是,在此函数内部声明的变量包含在该函数内部,不会影响该函数外部的任何代码或与该函数外部的任何代码交互。将任何类型的可重用插件包装在闭包中是一个很好的做法。

This is the basic syntax for creating a closure. More typically, it'd contain some code:

(function(){
    //Your Code Here
})();

This is equivalent to

var some_function = function() {
    //Your Code Here
};
some_function();

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.

神妖 2024-11-03 11:09:28

它立即执行匿名函数。
它基本上与:

var test = function(){};
test();

但不需要使用附加变量。
您需要将其括在额外的括号中才能将函数作为表达式的结果 - 否则它会被理解为函数声明,并且您无法执行声明。

它主要用于作用域保护 - 因为 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:

var test = function(){};
test();

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.

月竹挽风 2024-11-03 11:09:28

它称为立即函数匿名闭包,是模块模式

它用于为代码创建私有本地范围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文