论点位于词法环境中的什么位置?
Where are arguments positioned in the lexical environment?
无论是否存在同名变量,以下代码始终打印传递给参数 a
的参数。
大概是因为参数标识符分别绑定到范围内的变量。他们在哪里?他们在词法环境中吗?
function foo(a, b = () => a) {
var a = 1
console.log(b())
}
foo() // undefined
foo(2) // 2
是否 var
声明在特殊的 VariableEnvironment 中结束,而参数位于 LexicalEnvironment 中?并且 let
和 const
通过使重新定义成为早期错误来避免冲突?
相关的还有:
如果any default values are present,将为参数创建单独的环境记录。
在此位置声明的函数的语义使得此环境记录定义了它们的本地范围。 A note in the spec(见第 28 条)表示:
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.
来自 the spec 的更多内容:
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)用于参数绑定。也许吧。
无论是否存在同名变量,以下代码始终打印传递给参数 a
的参数。
大概是因为参数标识符分别绑定到范围内的变量。他们在哪里?他们在词法环境中吗?
function foo(a, b = () => a) {
var a = 1
console.log(b())
}
foo() // undefined
foo(2) // 2
是否 var
声明在特殊的 VariableEnvironment 中结束,而参数位于 LexicalEnvironment 中?并且 let
和 const
通过使重新定义成为早期错误来避免冲突?
相关的还有:
如果any default values are present,将为参数创建单独的环境记录。
在此位置声明的函数的语义使得此环境记录定义了它们的本地范围。 A note in the spec(见第 28 条)表示:
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.
来自 the spec 的更多内容:
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)用于参数绑定。也许吧。