- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
据说每个代码块都有一个名为 LexicalEnviroment
的隐藏对象。 .该对象包含对外部作用域的引用和 EnviromentRecord
,其中包含有关当前作用域的信息。
另一方面,据说函数能够闭包,这要归功于 [[Enviroment]]
构造“记住函数的定义位置”。
搞糊涂了,LexicalEnviroment
有什么关系对象和 [[Enviroment]]
?
他们是一回事吗?只有函数有[[Enviroment]]
构造?他们有 LexicalEnviroment
对象呢?
最佳答案
tl;博士
它们都是 Environment Record
的实例.LexicalEnvironment
只是执行上下文的一个组件(函数不能有 LexicalEnvironment
),并且在调用函数时,一个新的 LexicalEnvironment
为当前上下文创建,其 [[OuterEnv]]
字段被设置为函数 [[Environment]]
field 。
如果是 Javascript,我想应该是:
function handleInvokeFunction(func) {
const localEnv = new EnvironmentRecord();
localEnv.set('[[OuterEnv]]', func['[[Environment]]'])
calleeContext.lexicalEnvironment = localEnv;
}
this
值(value)。当然,这过于简单化
[src] .
Environment Record is a specification type used to define the association of Identifiers to specific variables and functions.
Each time such code is evaluated, a new Environment Record is created to record the identifier bindings that are created by that code.
// Environment Record "EnvA"
const hello = "world";
if (1) {
// Environment Record "EnvB"
console.log(hello);
}
// outputs: world
那是因为他们有一个名为
[[OuterEnv]]
的字段。 ,它指向父环境。所以在上面的例子中,
[[OuterEnv]]
“EnvB”字段设置为“EnvA”
[src] .
Every Environment Record has an
[[OuterEnv]]
field, which is either null or a reference to an outer Environment Record.
[[OuterEnv]]
新环境到旧(当前事件)环境的领域。 An execution context is a specification device that is used to track the runtime evaluation of code by an ECMAScript implementation.
The execution context stack is used to track execution contexts.
LexicalEnvironment
成分。需要跟踪该特定代码块中的变量。
LexicalEnvironment: Identifies the Environment Record used to resolve identifier references made by code within this execution context. [src]
LexicalEnvironment
是 一个环境记录,所以它有一个
[[OuterEnv]]
字段,这是运行时将相应更改的内容。
LexicalEnvironment
不属于函数对象。它只属于一个执行上下文。
The running execution context is always the top element of this stack.
[[OuterEnv]]
创建一个新的环境记录值(与以前相同的步骤)。 // This is Environment Record "EnvA".
// The [[OuterEnv]] field for "EnvA" is null.
// The running context LexicalEnvironment is "EnvA".
const hello = "world";
if (1) {
// Found new block
// Create a new Environment Record "EnvB".
// Set the "EnvB" [[OuterEnv]] field to
// the running context LexicalEnvironment.
// In this case, its "EnvA".
// Change the running context LexicalEnvironment to "EnvB".
// Evaluate all lines in the body using the new
// running context LexicalEnvironment.
// In this case, its "EnvB".
console.log(hello);
// Restore the previous running context LexicalEnvironment.
// Return the result.
}
// The running context LexicalEnvironment is Environment Record "A".
// Since the inner block restored before returning, it didn't change.
[[环境]]
[[Environment]]
出现。
[[Environment]]: The Environment Record that the function was closed over. Used as the outer environment when evaluating the code of the function.
LexicalEnvironment
存储为
[[Environment]]
函数对象的字段
[step 35]
[step 3]
[step 14] .
[[Environment]]
字段用作
[[OuterEnv]]
[step 10] .
[[Environment]]
中。 ,当被调用时,它可以使用
[[Environment]]
再次访问它们。 .
// This is Environment Record "EnvA".
// The [[OuterEnv]] field for "EnvA" is null.
// The running context LexicalEnvironment is "EnvA".
const hello = "world";
// Found a function, store the running context
// into its [[Environment]] field, and do nothing else.
function foo() {
// This block runs only after invoking bar().
// Create a new executing context "calleeContext".
// Create a new Environment Record "EnvB".
// Set the "EnvB" [[OuterEnv]] field, to the value
// stored inside [[Environment]]. In this case, its "EnvA".
// Set the LexicalEnvironment of "calleeContext" to "EnvB".
// Push "calleeContext" to the execution context stack.
// That makes "calleeContext" the running execution context.
// Evaluate all lines in the body
// using "calleeContext" LexicalEnvironment.
// In this case, its "EnvB".
// If a function is found here, set its
// [[Environment]] to "calleeContext" LexicalEnvironment.
console.log(hello); // works because `hello` was in "EnvA"
// Pop "calleeContext" from the execution context stack.
// "calleeContext" is no longer the running execution context.
// Return the result.
}
const bar = foo;
bar();
// The [[Environment]] of `bar` is still "EnvA".
// The running context LexicalEnvironment is still "EnvA".
由于该示例在声明它的同一环境中调用该函数,因此它实际上并未使用“闭包”,但您可能已经明白了。
[[Environment]]
和
LexicalEnvironment
是
Environment Records
,它们用于不同的东西。
[[Environment]]
持有
LexicalEnvironment
函数被声明的地方。
LexicalEnvironment
是执行上下文的一个组件,它存储有关该特定代码块中变量的信息。
关于javascript - LexicalEnviroment 对象和 [[Environment]] 之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64202695/
据说每个代码块都有一个名为 LexicalEnviroment 的隐藏对象。 .该对象包含对外部作用域的引用和 EnviromentRecord ,其中包含有关当前作用域的信息。 另一方面,据说函数能
我是一名优秀的程序员,十分优秀!