问题描述
以下代码始终打印传递给参数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中?并且let
和const
通过将重新定义作为早期错误来避免冲突?
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:
规范的更多信息:
因此,在没有默认参数的情况下,我推断出一个预先存在的词汇环境(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.
这篇关于词汇环境中的论点位于何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!