gpt4 book ai didi

javascript - 清楚 ECMAScript/JavaScript 中 "LexicalEnvironment"和 "VariableEnvironment"之间的区别

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:47 41 4
gpt4 key购买 nike

有人可以阐明这两者之间的区别吗,因为它们存在于执行上下文中?我很难阅读 ECMA 262 v 5规范并清楚地看到差异。

谢谢你,

最佳答案

两者都是执行上下文的组件(相同类型),但它们有不同的用途(from the spec):

词法环境

Identifies the Lexical Environment used to resolve identifier references made by code within this execution context.

可变环境

Identifies the Lexical Environment whose environment record holds bindings created by VariableStatements and FunctionDeclarations within this execution context.

下一段解释了为什么它们需要不同:

When an execution context is created its LexicalEnvironment and VariableEnvironment components initially have the same value. The value of the VariableEnvironment component never changes while the value of the LexicalEnvironment component may change during execution of code within an execution context.

这种情况并不经常发生,通常都指代相同的 Lexical Environment。问题 Why do catch clauses have their own lexical environment? 中给出了改变 LexicalEnvironment 的一个很好的例子。 - 参见 §12.14 .我可以在规范中找到发生这种情况的另一个地方是 With Statements ( §12.10 ),其中 Object Environment Record动态用于标识符解析 - 但变量/函数声明是静态的。

关于javascript - 清楚 ECMAScript/JavaScript 中 "LexicalEnvironment"和 "VariableEnvironment"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15031667/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com