ES6 中的块和执行环境
Blocks and execution environments in ES6
在 ES6 中,每个块都与一个 LexicalEnvironment
关联,以支持该语言版本的块作用域特性。
这是否意味着在 ES6 中每个块都会实例化一个新的 ExecutionContext
?
参考:http://globaldev.co.uk/2013/09/es6-part-2/
例如(我在这里故意使用函数范围的var
):
function foo() {
{
var foo; // Has a new ExecutionContext been instantiated here?
}
}
规范的相关位:https://people.mozilla.org/~jorendorff/es6-draft.html#sec-block-runtime-semantics-evaluation
不,每个函数只有一个执行环境。 The LexicalEnvironment simply temporarily replaces the current LexicalEnvironment:
- Let oldEnv be the running execution context’s LexicalEnvironment.
- Let blockEnv be NewDeclarativeEnvironment(oldEnv).
Perform BlockDeclarationInstantiation(StatementList, blockEnv).
- Set the running execution context’s LexicalEnvironment to blockEnv.
- Let blockValue be the result of evaluating StatementList.
- Set the running execution context’s LexicalEnvironment to oldEnv.
- Return blockValue.
Does this mean that in ES6 a new ExecutionContext is instantiated for
every block?
不,我认为情况并非如此。
根据我对规范的阅读,当前 ExecutionContext
的 LexicalEnvironment
暂时替换为为块实例化的 LexicalEnvironment
,在执行期间堵塞。 LexicalEnvironment
在控制离开块之前恢复。
在 ES6 中,每个块都与一个 LexicalEnvironment
关联,以支持该语言版本的块作用域特性。
这是否意味着在 ES6 中每个块都会实例化一个新的 ExecutionContext
?
参考:http://globaldev.co.uk/2013/09/es6-part-2/
例如(我在这里故意使用函数范围的var
):
function foo() {
{
var foo; // Has a new ExecutionContext been instantiated here?
}
}
规范的相关位:https://people.mozilla.org/~jorendorff/es6-draft.html#sec-block-runtime-semantics-evaluation
不,每个函数只有一个执行环境。 The LexicalEnvironment simply temporarily replaces the current LexicalEnvironment:
- Let oldEnv be the running execution context’s LexicalEnvironment.
- Let blockEnv be NewDeclarativeEnvironment(oldEnv). Perform BlockDeclarationInstantiation(StatementList, blockEnv).
- Set the running execution context’s LexicalEnvironment to blockEnv.
- Let blockValue be the result of evaluating StatementList.
- Set the running execution context’s LexicalEnvironment to oldEnv.
- Return blockValue.
Does this mean that in ES6 a new ExecutionContext is instantiated for every block?
不,我认为情况并非如此。
根据我对规范的阅读,当前 ExecutionContext
的 LexicalEnvironment
暂时替换为为块实例化的 LexicalEnvironment
,在执行期间堵塞。 LexicalEnvironment
在控制离开块之前恢复。