为什么匿名函数表达式和命名函数表达式初始化如此不同? [英] Why are anonymous function expressions and named function expressions initialized so differently?

查看:124
本文介绍了为什么匿名函数表达式和命名函数表达式初始化如此不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看第13节或ECMAScript规范(v。5)。一个匿名函数表达式的初始化如下:

I'm looking at section 13 or the ECMAScript specification (v. 5). An anonymous function expression is initialized as follows:


返回按照13.2的规定创建一个新的Function对象的结果,其参数由FormalParameterListopt指定,由FunctionBody指定的主体。作为范围传递正在运行的执行上下文的LexicalEnvironment。如果FunctionExpression包含在严格代码中,或者其FunctionBody是严格代码,则传入true作为Strict标志。

Return the result of creating a new Function object as specified in 13.2 with parameters specified by FormalParameterListopt and body specified by FunctionBody. Pass in the LexicalEnvironment of the running execution context as the Scope. Pass in true as the Strict flag if the FunctionExpression is contained in strict code or if its FunctionBody is strict code.

这个逻辑是<非常类似于函数声明的初始化。但是,请注意命名函数表达式的不同初始化是如何进行的。

this logic is very similar to how a function declaration is initialized. However, notice how different initialization of a named funciton expression is.



  1. 让funcEnv成为调用NewDeclarativeEnvironment传递正在运行的执行上下文的词法环境作为
    参数
  2. 让envRec成为funcEnv的环境记录。
  3. 调用CreateImmutableBinding envRec的具体方法传递标识符的字符串值作为参数。
  4. 让闭包是创建一个如13.2中所指定的新函数对象的结果,其参数由FormalParameterListopt指定,
    和由FunctionBody指定的正文。作为范围传递funcEnv。如果FunctionExpression包含在
    严格代码中,或者其FunctionBody是严格代码,则传递
    作为Strict标志。

  5. 调用envRec传递的InitializeImmutableBinding具体方法标识符和闭包的字符串值作为参数。
  6. 返回结束。
  1. Let funcEnv be the result of calling NewDeclarativeEnvironment passing the running execution context’s Lexical Environment as the argument
  2. Let envRec be funcEnv’s environment record.
  3. Call the CreateImmutableBinding concrete method of envRec passing the String value of Identifier as the argument.
  4. Let closure be the result of creating a new Function object as specified in 13.2 with parameters specified by FormalParameterListopt and body specified by FunctionBody. Pass in funcEnv as the Scope. Pass in true as the Strict flag if the FunctionExpression is contained in strict code or if its FunctionBody is strict code.
  5. Call the InitializeImmutableBinding concrete method of envRec passing the String value of Identifier and closure as the arguments.
  6. Return closure.


我知道命名/匿名函数表达式之间的一个很大的区别是命名函数表达式可以从函数内部递归调用,但这就是我能想到的。为什么设置如此不同以及为什么需要执行这些额外的步骤?

I know one of the big differences between named/anonymous function expressions is that named function expressions can be called recursively from within the function, but that's all I can think of. Why is the setup so different and why does it need to do those extra steps?

推荐答案

所有跳舞

命名函数表达式的标识符需要在函数范围内可用,但不能在之外

The identifier of named function expression needs to be made available within function scope but not outside.

typeof f; // undefined

(function f() {
  typeof f; // function
})();

你如何在函数中使用 f

How do you make f available within function?

由于 f 不应在外部可用,因此不能在外部词法环境中创建绑定。而且你不能在内部变量环境中创建绑定,因为......它尚未创建;这个函数在实例化的时候还没有执行,所以10.4.3(进入函数代码)步与它的NewDeclarativeEnvironment从来没有发生过。

You can't create binding in the outer Lexical Environment since f shouldn't be available outside. And you can't create binding in the inner Variable Environment since... it's not yet created; the function is not yet executed at the moment of instantiation, and so 10.4.3 (Entering Function Code) step with its NewDeclarativeEnvironment has never happened.

所以这是通过创建一个直接从当前继承的中间词法环境,然后将其作为[[Scope]]传递到新创建的函数中。

So the way this is done is by creating an intermediate lexical environment that "inherits" directly from current one, and which is then passed as [[Scope]] into the newly created function.

如果我们将13中的步骤分解为伪代码,您可以清楚地看到这一点:

You can see this clearly if we break steps in 13 into pseudo code:

// create new binding layer
funcEnv = NewDeclarativeEnvironment(current Lexical Environment)

envRec = funcEnv
// give it function's identifier
envRec.CreateImmutableBinding(Identifier)

// create function with this intermediate binding layer
closure = CreateNewFunction(funcEnv)

// assign newly created function to an identifier within this intermediate binding layer
envRec.InitializeImmutableBinding(Identifier, closure)

因此, f 中的词法环境(例如解析标识符)现在看起来像这样:

So the lexical environment within f (when resolving identifier, for example) now looks like this:

(function f(){

  [global environment] <- [f: function(){}] <- [Current Variable Environment]

})();

使用匿名函数它看起来像这样:

With anonymous function it would look like this:

(function() {

  [global environment] <- [Current Variable Environment]

})();

这篇关于为什么匿名函数表达式和命名函数表达式初始化如此不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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