VarDeclaredNames 和 VarScopedDeclarations 之间有什么区别?

What's the difference between VarDeclaredNames and VarScopedDeclarations?

我正在阅读 EcmaScript 规范。

9.2.12,有:

11.Let varNames be the VarDeclaredNames of code. 
12.Let varDeclarations be the VarScopedDeclarations of code.

在 13.1.5 和 13.1.6:

13.1.5 Static Semantics: VarDeclaredNames

Statement :

EmptyStatement
ExpressionStatement
ContinueStatement
BreakStatement
ReturnStatement
ThrowStatement
DebuggerStatement
Return a new empty List.

13.1.6 Static Semantics: VarScopedDeclarations

Statement :

EmptyStatement
ExpressionStatement
ContinueStatement
BreakStatement
ReturnStatement
ThrowStatement
DebuggerStatement
Return a new empty List.

它们看起来像 same.So 我想知道 VarDeclaredNamesVarScopedDeclarations 之间有什么区别?你能给我一些例子吗?

谢谢。

这两个静态语义规则在 AST 中搜索相同类型的东西:VariableDeclarations,ForBindings,FunctionDeclarations 和 GeneratorDeclarations。确实有很多重复(尤其是在方法上)。

然而,正如@loganfsmyth 在评论中提到的那样,他们return 处理不同的数据——不同类型的列表。 VarDeclaredNames return 是名称列表(字符串),VarScopedDeclarations 是 return 声明列表(即 AST 节点)。

这在实际附加到列表的部分中很明显:§13.3.2.2, §13.7.4.5, §13.7.5.7, and §13.2.9 all do refer to the BoundNames of the respective element, while §13.3.2.3, §13.7.4.6, §13.7.5.8, and §13.2.10确实引用了相应的声明本身。

为什么需要这种区分? VarDeclaredNames 用于在范围内创建绑定,而 VarScopedDeclarations 用于查找要创建的函数声明(并使用这些值初始化绑定)。

还能再简单点吗?是的,当然 - 对于词法声明,scope initialisation description just iterates the declarations and gets the BoundNames of each. You might want to submit a bug to spec authors to use this approach for function-level declarations as well. See issue 1203 讨论这个。