词汇环境中的论点位于何处? [英] Where are arguments positioned in the lexical environment?

查看:82
本文介绍了词汇环境中的论点位于何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码始终打印传递给参数a的参数,而不管是否存在相同名称的变量.

The following code always prints the argument passed in to parameter a, regardless of the presence of a variable with the same name.

大概是因为参数标识符分别绑定到作用域中的变量.他们的位置在哪里?他们在词汇环境中吗?

Presumably because parameter identifiers are bound separately to variables in scope. Where are they positioned? Are they in the lexical environment?

function foo(a, b = () => a) {
  var a = 1
  console.log(b())
}
foo() // undefined
foo(2) // 2

var声明是否以特殊的 VariableEnvironment 结尾,而参数放置在LexicalEnvironment中?并且letconst通过将重新定义作为早期错误来避免冲突?

Is it that var declarations end up in the special VariableEnvironment, while parameters are positioned in the LexicalEnvironment? And let and const avoid conflict by making redefinition an early error?

也相关:

  • 8.3.2 ResolveBinding(name [, env])
  • 8.1.1 Environment Records

推荐答案

如果任何默认值值存在,将为参数创建一个单独的环境记录.

In the event that any default values are present, a separate environment record is created for parameters.

在此位置声明的函数的语义使该环境记录定义其局部范围.规范中的注释(请参见第28条)说:

The semantics of functions declared in this position are such that this environment record defines their local scope. A note in the spec (see clause 28) says:

注意:需要单独的环境记录,以确保由形式参数列表中的表达式创建的闭包在函数体中不具有声明的可见性.

NOTE: A separate Environment Record is needed to ensure that closures created by expressions in the formal parameter list do not have visibility of declarations in the function body.

规范的更多信息:

当建立用于评估ECMAScript函数的执行上下文时,将创建一个新函数Environment Record,并在该Environment Record中实例化每个形式参数的绑定.函数体中的每个声明也都被实例化.如果函数的形式参数不包含任何默认值初始化程序,则主体声明将在与参数相同的环境记录中实例化.如果存在默认值参数初始化程序,则会为主体声明创建第二个环境记录.形式参数和函数作为FunctionDeclarationInstantiation的一部分进行初始化.所有其他绑定都在函数主体评估期间初始化.

When an execution context is established for evaluating an ECMAScript function a new function Environment Record is created and bindings for each formal parameter are instantiated in that Environment Record. Each declaration in the function body is also instantiated. If the function's formal parameters do not include any default value initializers then the body declarations are instantiated in the same Environment Record as the parameters. If default value parameter initializers exist, a second Environment Record is created for the body declarations. Formal parameters and functions are initialized as part of FunctionDeclarationInstantiation. All other bindings are initialized during evaluation of the function body.

因此,在没有默认参数的情况下,我推断出一个预先存在的词汇环境(VariableEnvironment或LexicalEnvironment)用于参数绑定.也许.

In the absence of default arguments, therefore, I deduce that one of the pre-existing lexical environments (VariableEnvironment or LexicalEnvironment) is used for parameter bindings. Maybe.

这篇关于词汇环境中的论点位于何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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