解释封装的匿名函数语法 [英] Explain the encapsulated anonymous function syntax

查看:118
本文介绍了解释封装的匿名函数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能解释一下JavaScript中封装的匿名函数语法背后的原因吗?为什么这样做:(function(){})(); 但这不是: function(){}();

Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn't: function(){}();?

在JavaScript中,可以创建一个这样的命名函数:

In JavaScript, one creates a named function like this:

function twoPlusTwo(){
    alert(2 + 2);
}
twoPlusTwo();

您还可以创建匿名函数并将其分配给变量:

You can also create an anonymous function and assign it to a variable:

var twoPlusTwo = function(){
    alert(2 + 2);
};
twoPlusTwo();

您可以通过创建匿名函数来封装代码块,然后将其包装在括号中并执行它立即:

You can encapsulate a block of code by creating an anonymous function, then wrapping it in brackets and executing it immediately:

(function(){
    alert(2 + 2);
})();

这在创建模块化脚本时很有用,可以避免使当前范围或全局范围混乱可能存在冲突的变量 - 如Greasemonkey脚本,jQuery插件等。

This is useful when creating modularised scripts, to avoid cluttering up the current scope, or global scope, with potentially conflicting variables - as in the case of Greasemonkey scripts, jQuery plugins, etc.

现在,我理解为什么会这样。括号括起内容并仅揭示结果(我确定有更好的方法来描述),例如(2 + 2)=== 4

Now, I understand why this works. The brackets enclose the contents and expose only the outcome (I'm sure there's a better way to describe that), such as with (2 + 2) === 4.

但我不喜欢不明白为什么这也不能同样有效:

But I don't understand why this does not work equally as well:

function(){
    alert(2 + 2);
}();

你能解释一下吗?

推荐答案

它不起作用,因为它被解析为 FunctionDeclaration ,以及名称标识符函数声明是必需的

It doesn't work because it is being parsed as a FunctionDeclaration, and the name identifier of function declarations is mandatory.

当用括号括起来时,它被评估为 FunctionExpression ,函数表达式可以命名与否。

When you surround it with parentheses it is evaluated as a FunctionExpression, and function expressions can be named or not.

FunctionDeclaration 看起来像这样:

function Identifier ( FormalParameterListopt ) { FunctionBody }

FunctionExpression s:

function Identifieropt ( FormalParameterListopt ) { FunctionBody }

正如你所看到的那样标识符(标识符 opt )令牌在 FunctionExpression 是可选的,因此我们可以拥有一个功能Ë没有定义名称的xpression:

As you can see the Identifier (Identifieropt) token in FunctionExpression is optional, therefore we can have a function expression without a name defined:

(function () {
    alert(2 + 2);
}());

命名函数表达式:

(function foo() {
    alert(2 + 2);
}());

圆括号(正式称为分组运算符)只能包含表达式,并且会计算函数表达式。

The Parentheses (formally called the Grouping Operator) can surround only expressions, and a function expression is evaluated.

这两个语法产生可能不明确,它们看起来完全相同,例如:

The two grammar productions can be ambiguous, and they can look exactly the same, for example:

function foo () {} // FunctionDeclaration

0,function foo () {} // FunctionExpression

解析器知道它是 FunctionDeclaration 还是 FunctionExpression ,具体取决于它出现的上下文

The parser knows if it's a FunctionDeclaration or a FunctionExpression, depending on the context where it appears.

在上面的例子中,第二个是表达式,因为逗号运算符也只能处理表达式。

In the above example, the second one is an expression because the Comma operator can also handle only expressions.

另一方面, FunctionDeclaration s实际上只能出现在calle中d Program 代码,表示全局范围之外的代码,以及其他函数的 FunctionBody 内部。

On the other hand, FunctionDeclarations could actually appear only in what's called "Program" code, meaning code outside in the global scope, and inside the FunctionBody of other functions.

应该避免块内的函数,因为它们可能导致不可预测的行为,例如:

Functions inside blocks should be avoided, because they can lead an unpredictable behavior, e.g.:

if (true) {
    function foo () { alert('true'); }
} else {
    function foo () { alert('false!'); }
}

foo(); // true? false? why?

上面的代码实际上应该产生 SyntaxError ,因为 阻止 只能包含语句(并且ECMAScript规范没有定义任何函数语句),但大多数实现都是宽容的,并且只需要第二个函数,即警告'false!'

The above code should actually produce a SyntaxError, since a Block can only contain statements (and the ECMAScript Specification doesn't define any function statement), but most implementations are tolerant, and will simply take the second function, the one which alerts 'false!'.

Mozilla实现-Rhino,SpiderMonkey, - 有不同的行为。他们的语法包含一个非标准的函数语句,这意味着该函数将在运行时进行评估,而不是在分析时进行评估,因为它与 FunctionDeclaration 秒。在这些实现中,我们将定义第一个函数。

The Mozilla implementations -Rhino, SpiderMonkey,- have a different behavior. Their grammar contains a non-standard Function Statement, meaning that the function will be evaluated at run-time, not at parse time, as it happens with FunctionDeclarations. In those implementations we will get the first function defined.

函数可以用不同的方式声明,比较以下

Functions can be declared in different ways, compare the following:

1-使用分配给功能构造函数定义的函数变量乘法

1- A function defined with the Function constructor assigned to the variable multiply:

var multiply = new Function("x", "y", "return x * y;");

2-名为乘法的函数的函数声明

function multiply(x, y) {
    return x * y;
}

3-分配给变量的函数表达式

var multiply = function (x, y) {
    return x * y;
};

4-命名函数表达式 func_name ,分配给变量乘以

4- A named function expression func_name, assigned to the variable multiply:

var multiply = function func_name(x, y) {
    return x * y;
};

这篇关于解释封装的匿名函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆