为什么变量对象在ES5中被改为词汇环境? [英] Why variable object was changed to lexical environment in ES5?

查看:122
本文介绍了为什么变量对象在ES5中被改为词汇环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ES5将变量对象(VO)更改为词汇环境。这种变化的动机是什么,因为VO作为感知已经非常明显了?

ES5 changed variable object(VO) to lexical environment. What's the motivation of such change since VO is already very obvious as perception?

推荐答案

我认为变量对象更像是环境记录

I think variable objects are more analogous to environment records.


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

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

在ES5中有两种不同的环境记录:

In ES5 there are two different kinds of environment records:


声明性环境记录用于定义
ECMAScript语言语法元素(如 FunctionDeclarations
VariableDeclarations Catch 子句)直接关联标识符绑定的效果使用ECMAScript语言值。 Object
环境记录用于定义ECMAScript
元素的效果,例如 Program WithStatement ,它们将
标识符绑定与属性相关联某些对象。

Declarative environment records are used to define the effect of ECMAScript language syntactic elements such as FunctionDeclarations, VariableDeclarations, and Catch clauses that directly associate identifier bindings with ECMAScript language values. Object environment records are used to define the effect of ECMAScript elements such as Program and WithStatement that associate identifier bindings with the properties of some object.

所以问题是为什么引入声明性环境记录而不是仅使用对象环境记录,就像ES3变量对象一样。区别在于声明性环境记录可以有不可变绑定:

So the question would be why declarative environment records were introduced instead of only using object environment records just like ES3 variable objects. The difference is that declarative environment records can have immutable bindings:


除了所有Environment
Records支持的可变绑定外,声明性环境记录还提供了不可变的
绑定。不可变绑定是指
标识符与值之间的关联,一旦建立
就不能修改。

In addition to the mutable bindings supported by all Environment Records, declarative environment records also provide for immutable bindings. An immutable binding is one where the association between an identifier and a value may not be modified once it has been established.

不可变绑定在对象中没有直接的等价物。属性可以定义为不可配置和不可写,变为不可变。但是,

Immutable bindings don't have a direct equivalent in objects. A property can be defined as both non-configurable and non-writable, becoming immutable. However,


创建和初始化不可变绑定是不同的步骤所以
这样的绑定有可能存在于初始化中或
未初始化状态。

Creation and initialisation of immutable binding are distinct steps so it is possible for such bindings to exist in either an initialised or uninitialised state.

但您不能拥有未初始化的财产。如果您定义一个值不可定义的不可配置的不可写属性,那么您将无法将其初始化为所需的值。

But you can't have an uninitialized property. If you define a non-configurable non-writable property with value undefined, then you won't be able to initialize it to the desired value.

我不认为在ES5中可以使用未初始化的不可变绑定。 CreateImmutableBinding仅用于声明绑定实例化函数定义,在这两种情况下,它都会立即用InitializeImmutableBinding初始化。

I don't think it's possible to have uninitialized immutable bindings in ES5. CreateImmutableBinding is only used in Declaration Binding Instantiation and Function Definition, and in both cases it's immediately initialized with InitializeImmutableBinding.

但可能这样做是为了允许未初始化的不可变绑定作为语言的扩展,如JavaScript 1.5 const 。或许他们已经考虑过ES6 const

But possibly this was done to allow uninitialized immutable bindings as extensions of the language, like the JavaScript 1.5 const. Or maybe they already had in mind ES6 const.

这篇关于为什么变量对象在ES5中被改为词汇环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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