问题
I'm reading EcmaScript specification.
At 9.2.12, there are:
11.Let varNames be the VarDeclaredNames of code.
12.Let varDeclarations be the VarScopedDeclarations of code.
And at 13.1.5 and 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.
They look like the same.So I want to know what's the difference between VarDeclaredNames
and VarScopedDeclarations
? Can you give me some examples?
Thanks.
回答1:
Those two static semantic rules search the AST for the same kinds of things: VariableDeclarations, ForBindings, FunctionDeclarations and GeneratorDeclarations. There's a lot of duplication (especially in methodology) indeed.
However, as @loganfsmyth mentions in the comments, they do return different data - different types of lists. While VarDeclaredNames
returns a list of names (strings), VarScopedDeclarations
does return a list of declarations (i.e. AST nodes).
This is apparent in the sections where something is actually appended to the lists: §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 do refer to the respective declaration itself.
Why is this distinction needed? The VarDeclaredNames
are used to create the bindings in the scope, while the VarScopedDeclarations
are used to find the function declarations to create (and initialise the bindings with those values).
Could it have been simpler? Yes, surely - for lexical declarations, the 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 discussing this.
来源:https://stackoverflow.com/questions/40609675/whats-the-difference-between-vardeclarednames-and-varscopeddeclarations