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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/js/429098.html

猜你在找的JavaScript相关文章