什么是声明性环境记录,它与激活对象有何不同? [英] What really is a declarative environment record and how does it differ from an activation object?

查看:167
本文介绍了什么是声明性环境记录,它与激活对象有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我最近一直在阅读ES-5词汇环境范围,我不确定我是否真的了解变量在EcmaScript中的存储方式。我做了一些研究,但没有澄清我的信息,只给我带来了两个问题。所以它们是:

Okay, so I lately have been reading about ES-5 lexical environment scope and I am not sure if I really understand what is going on with how variables are stored in EcmaScript. I did some research but it didn't clarified my information, only brought me up to two questions. So there they are:


  1. 第一个是关于ES-3 激活对象 / 变量对象。在阅读了ES-3规范和Internet上的一些资源后,我可以假设它们只是普通对象,例如像 new Object 创建的那些,但没有一个消息来源说是的,这只是一个普通的对象直接。此外,Dmitry Soshnikov在他的博客上写道(重点是我的):

  1. The first one is about ES-3 activations objects/variable objects. After reading ES-3 Specification and some sources on the Internet I can assume that they are just normal object, for example like those created by new Object, but none of the sources says "yes, this is just a plain object" directly. Moreover, Dmitry Soshnikov wrote on his blog (the emphasis is mine):


原理图和示例,它可以将变量对象呈现为普通的ECMAScript对象

Schematically and for examples, it is possible to present variable object as a normal ECMAScript object

并且该引用不能让我确定什么是真正的激活对象是。所以这是第一个问题:激活对象是一个常规的EcmaScript对象吗?如果没有,那么它又是什么呢?

and that quotation doesn't let me be sure about what really an activation object is. So this is the first question: is an activation object a regular EcmaScript object? If not, then what is it otherwise?

在ES-5中我们现在有对象环境记录 ,它似乎或多或少与ES-3激活对象相同,而声明性环境记录,它取代了函数和try-catch语句中的激活对象。因此,假设对象环境记录 只是简单的EcmaScript对象,那么什么是声明性环境记录?规范并没有澄清这一点,而且,从我在那里读到的,我无法想象这不是作为一个对象实现的。因此,如果声明性环境记录不是ES对象,那么它们是什么以及它们在代码执行期间如何实现和表示?

In ES-5 we now have object environment records, which seem to be more or less the same that ES-3 activation objects, and declarative environment records, which replaced activation objects in functions and try-catch statement. So, assuming that object environment records are just plain EcmaScript objects, then what is a declarative environment record? The specification doesn't clarify this, furthermore, from what I've read there I cannot imagine that this is not implemented as an object. So, again, if declarative environment records are not ES objects, then what are they and how are they implemented and represented during the code execution?

非常感谢你为我提供这个主题。

Thank you very much in advance for brightening that topic for me.

编辑:我想我需要澄清一下是这个问题。我想知道的主要事情是激活对象/对象环境记录和声明性环境记录之间的确切区别。这就是我最感兴趣的。

EDIT: I think I need to clarify what is this question about. The main thing that I want to know is what is the exact difference between activation objects/object environment records and declarative environment records. That's what I'm interested in most.

推荐答案

首先,你必须要知道所有这些术语只是描述概念。他们没有规定任何形式的实施。但是因为这很难想象/可视化,所以可以帮助将这些概念视为您知道的事物的实例化,例如地图或表格。

First of all you have to be aware that all of these terms just describe concepts. They don't dictate any kind of implementation. But because this can be hard to imagine/visualize it can be helpful to think about these concepts as instantiations of something you know, like maps or tables.

声明性环境记录(DER)和对象环境记录(OER)有一个共同点:它们都是环境记录(ER ),在规范中定义为:

Declarative environment records (DER) and object environment records (OER) have one thing in common: They are both environment records (ER), which are defined in the specification as:


环境记录记录在其关联的词法环境范围内创建的标识符绑定。

An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment.

基本上是指ER跟踪变量和函数名称及其相关值。

This basically means that an ER keeps track of variable and function names and their associated values.

考虑这个例子:

var foo = 42;
function bar() { };

相应的ER将有两个条目,一个用于 foo 和一个 bar 。如果您想将ER作为一个表,那么它看起来像

The corresponding ER would have two entries, one for foo and one for bar. If you imagine an ER to be a table, then it would look like

  name        value
----------------------
  foo          42
  bar    <function object>

现在介绍DER和OER之间的区别。 DER可能是最容易理解的。

Now on to the difference between DER and OER. A DER might be the easiest to understand.

声明性环境记录

术语声明性应该听起来很熟悉,因为我们经常谈论变量声明函数声明。规范说:

The term declarative should sound familiar since we are often talking of variable declarations and function declarations. The specification says:


每个声明性环境记录都与包含变量和/或函数声明的ECMAScript程序范围相关联。声明性环境记录绑定由其范围内包含的声明定义的标识符集。

Each declarative environment record is associated with an ECMAScript program scope containing variable and/or function declarations. A declarative environment record binds the set of identifiers defined by the declarations contained within its scope.

所以,当你看到

var foo = 42;

function bar() {

}

然后你可以假设他们的名字和值存储在DER中。

then you can assume that their names and values are stored in a DER.

对象环境记录

OER不太常见,但在每个JS应用程序中至少存在一个OER。规范将其描述为

OERs are less common, but in each JS application there exist at least one OER. The specification describes it as


每个对象环境记录都与一个称为其绑定对象的对象相关联。对象环境记录绑定直接对应于其绑定对象的属性名称的标识符名称集。

Each object environment record is associated with an object called its binding object. An object environment record binds the set of identifier names that directly correspond to the property names of its binding object.

你有没有想过为什么窗口对象的属性是全局变量?这是因为全局范围的ER是OER:窗口绑定对象,并且对于每个属性,在其中创建相应的条目。 OER。这也在规范中:

Have you ever wondered why the properties of the window object are global variables? That's because the ER of the global scope is an OER: window is the binding object and for each of its properties a corresponding entry is created in the OER. This is also in the specification:


全局环境的环境记录是一个对象环境记录,其绑定对象是全局对象。

The global environment’s Environment Record is an object environment record whose binding object is the global object.

以下是一个示例:让我们假设绑定对象是

Here is an example: Lets assume out binding object is

var obj = {
   answer: 42
};

然后OER将是

    name        value
------------------------
    answer       42

请注意,在这种情况下,绑定对象( obj )确实是一个JavaScript对象。使用语句时,情况相同:

Note that in this case, the binding object (obj) is really a JavaScript object. You are in the same situation when you are using the with statement:

var obj = { foo: 42; };

with (obj) {
    foo = foo / 2;
}

console.log(obj);

with 创建一个OER并填充它传递对象的属性名称和值。这就是为什么你可以通过 obj。* 明确地引用它们来访问它们。如果将绑定对象分配给其中一个标识符,则OER还确保使用新值更新绑定对象。

with creates a OER and populates it with the property names and values from the passed object. That's why you can access them without explicitly referring to them via obj.*. The OER also makes sure to update the binding object with the new value if one was assigned to one of the identifiers.

激活对象

在ES3中看起来像激活对象(AO),在执行函数时自动创建并且它持有引用到特殊的参数对象。这似乎与DER有关,但仍然是独立的。

在ES5中似乎不存在AO的概念,我认为这是不必要的,因为 arguments 可以直接添加到执行上下文的DER中。

It looks like that in ES3, activation objects (AO) where automatically created when a function was executed and it was holding a reference to the special arguments object. This seems to be related to DERs, but still to be something independent.
The concept of AOs doesn't seem to exist anymore in ES5 and I assume that it was unnecessary, since arguments can be added directly to the DER of the execution context.

执行上下文

每当执行一个函数时,都会建立一个新的执行上下文(EC),并用于跟踪执行的状态:

A new execution context (EC) is established whenever a function is executed and is used to keep track of the state of the execution:


执行上下文包含跟踪其关联代码执行进度所需的任何状态。

An execution context contains whatever state is necessary to track the execution progress of its associated code.

这意味着引擎可以添加跟踪执行进度所需的任何信息。但规范还定义了EC 必须拥有的组件,其中一个是 VariableEnvironment ,它是一个ER(可能总是一个DER,但我不知道当然)。这意味着ER是EC的部分

This means the engine can add whatever information it needs to track the execution progress. But the specification also defines components that an EC must have, one of which is the VariableEnvironment, which is an ER (probably always a DER, but I don't know for sure). That means an ER is a part of an EC.

这篇关于什么是声明性环境记录,它与激活对象有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆