简体   繁体   English

执行上下文JavaScript的可变环境

[英]Variable environment of execution context JavaScript

In the sec. 秒。 10.3 describes the components of execution context as the follwoing: 10.3将执行上下文的组件描述如下:

The LexicalEnvironment and VariableEnvironment components of an execution context are always Lexical Environments. 执行上下文的LexicalEnvironment和VariableEnvironment组件始终是Lexical Environments。 When an execution context is created its LexicalEnvironment and VariableEnvironment components initially have the same value. 创建执行上下文时,其LexicalEnvironment和VariableEnvironment组件最初具有相同的值。 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. VariableEnvironment组件的值永远不会更改,而LexicalEnvironment组件的值可能会在执行上下文中执行代码期间更改。

Ok, let we have code snippet: 好吧,让我们有代码片段:

{
    alert(o.prop);
    var o={prop: 'prop'};
}

My understanding of this code snippet: 我对以下代码段的理解:

When control is transferred to this code snippet a corresponding execution context will be created and pushed to stack. 当控制权转移到此代码段时,将创建一个相应的执行上下文并将其压入堆栈。 This context is become a running execution context (called cont ). 该上下文将成为运行中的执行上下文(称为cont )。 During the cont is creating the VariableEnvironment of cont is creating. cont是创建VariableEnvironmentcont创造。 After the cont 's creation this code starting to execute. cont创建之后,此代码开始执行。

But as said in sec.10.3 但正如第10.3节所述

The value of the VariableEnvironment component never changes VariableEnvironment组件的值永不变

Thus we can assume that the VariableEnvironment never changed after creation of cont . 因此,我们可以假设在创建cont之后, VariableEnvironment从未更改。 Ie environment record of cont 's VariableEnvironment contains all binding created by VariableStatement and FunctionDeclarartion initially. contVariableEnvironment环境记录包含最初由VariableStatementFunctionDeclarartion创建的所有绑定。 Hence we can apply to object by reference o . 因此,我们可以通过引用o应用于对象。 But I have TypeError: o is undefined . 但是我有TypeError: o is undefined

Question: Why TypeError , described above is caused? 问题:为什么导致上述TypeError I'm expected that prop will be displayed with alert message because the value of the VariableEnvironment component never changes hence environment record of the VariableEnvironment never changes, hence all bindings of this record is immutable. 我预计prop将与警报消息一起显示,因为the value of the VariableEnvironment component never changes不会更改,因此VariableEnvironment环境记录不会更改,因此该记录的所有绑定都是不可变的。

Might I dont understand mean of value of EnvironmentRecord correctly? 我可能无法正确理解value of EnvironmentRecordvalue of EnvironmentRecord平均值?

First, your code is just definining a block , and JavaScript doesn't have block scope; 首先,您的代码只是定义一个block ,而JavaScript没有block范围; environment records are associated with global scope and functions, not blocks. 环境记录与全局范围和功能而不是块关联。

So let's assume that's actually a function so it creates a new scope and environment record: 因此,我们假设它实际上是一个函数,因此它将创建一个新的作用域和环境记录:

function foo()
{
    alert(o.prop);
    var o={prop: 'prop'};
}

That's treated exactly as though it were this: 完全就像是这样对待:

function foo()
{
    var o;
    o = undefined;
    alert(o.prop);
    o={prop: 'prop'};
}

Which should clarify things a bit. 哪个应该澄清一点。 :-) You're trying to dereference o as though it referred to an object, but it doesn't (yet); :-)您试图取消引用o就像它引用了一个对象一样,但是还没有(尚未); its value is undefined . 其值是undefined

This is because all var declarations are processed upon entry to the execution context, and all variables are initialized to undefined , before any step-by-step work is done. 这是因为在完成任何分步工作之前,所有var声明都会在进入执行上下文时进行处理,并且所有变量都将初始化为undefined Any initializer associated with a var (eg, var o = ... ) is really an assignment, and happens later when that code is reached in the step-by-step execution of the code. var相关联的任何初始化程序 (例如, var o = ... )实际上都是一个赋值,并且稍后在逐步执行代码时到达该代码时发生。

More (on my blog) : 更多(在我的博客上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM