javascript – 为什么变量对象在ES5中被改为词汇环境?

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

最佳答案
我认为变量对象更类似于environment records.

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

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

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变量对象那样的对象环境记录.区别在于declarative 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.

不可变绑定在对象中没有直接等价物.属性可以定义为不可配置和不可写,变为不可变.然而,

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.

但你不能拥有未初始化的财产.如果定义值为undefined的不可配置的不可写属性,则无法将其初始化为所需的值.

我认为在ES5中不可能有未初始化的不可变绑定. CreateImmutableBinding仅在Declaration Binding InstantiationFunction Definition中使用,并且在两种情况下都使用InitializeImmutableBinding立即初始化.

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

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面