词法环境是一种规范类型,用于根据 ECMAScript 代码的词法嵌套结构,来定义标识符与特定变量和函数的关联。一个词法环境由一个环境记录和一个可能为空的外部词法环境引用组成。词法环境由环境记录(Environment Record)和一个可能为 null 引用的外部词法环境(outer Lexical Environment)组成。通常词法环境与 ECMAScript 代码的某些特定的语法结构(例如,函数声明,块语句或者 Try 语句中的 Catch 语句)相关联,并且每次 evaluated 这类代码时,都会创建一个新的词法环境。

    ps:evaluated 不知道怎么翻译,暂且认为是解析代码的阶段

    A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.

    环境记录记录了在其关联的词法环境的范围内标识符绑定。它被称为词法环境的 EnvironmentRecord。

    An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment. It is referred to as the Lexical Environment’s EnvironmentRecord.

    外部环境引用是用来模拟词法环境值的逻辑嵌套。一个(内部)词法环境的外部引用是对逻辑上围绕着内部词法环境的词法环境的引用。当然,外部词法环境也可以有自己的词法环境。一个词法环境可以作为多个内部词法环境的外部环境。例如,如果一个函数声明包含两个嵌套的函数声明,那么每个嵌套的函数将以周围函数的当前 evaluation 的词法环境作为它的外部词法环境。

    ps:这部分标准写的非常啰嗦,其实就是几个函数如果嵌套了,那么当前函数的会把外面一层函数作为 outer 环境记录。

    The outer environment reference is used to model the logical nesting of Lexical Environment values. The outer reference of a (inner) Lexical Environment is a reference to the Lexical Environment that logically surrounds the inner Lexical Environment. An outer Lexical Environment may, of course, have its own outer Lexical Environment. A Lexical Environment may serve as the outer environment for multiple inner Lexical Environments. For example, if a FunctionDeclaration contains two nested FunctionDeclarations then the Lexical Environments of each of the nested functions will have as their outer Lexical Environment the Lexical Environment of the current evaluation of the surrounding function.

    全局环境是一个没有外部环境的词法环境。全局环境的外部环境为 null。全局环境的 EnvironmentRecord 可能预填充了标识符绑定,并且包含一个关联的全局对象,改对象的属性提供了某些全局环境的标识符绑定。在执行 ECMAScript 代码时,全局对象可能会被添加额外的属性,初始属性也可能被修改。

    A global environment is a Lexical Environment which does not have an outer environment. The global environment’s outer environment reference is null. A global environment’s EnvironmentRecord may be prepopulated with identifier bindings and includes an associated global object whose properties provide some of the global environment’s identifier bindings. As ECMAScript code is executed, additional properties may be added to the global object and the initial properties may be modified.

    模块环境是一个词法环境,它包含了模块顶层声明的绑定。它还包括了模块显式导入的绑定。模块环境的外部环境是一个全局环境。

    A module environment is a Lexical Environment that contains the bindings for the top level declarations of a Module. It also contains the bindings that are explicitly imported by the Module. The outer environment of a module environment is a global environment.

    函数环境是一个词法环境,它与 ECMAScript 函数对象的调用相对应。一个函数环境可以建立一个新的 this 绑定。一个函数环境还可以捕获支持 super 方法调用所需要的状态。

    A function environment is a Lexical Environment that corresponds to the invocation of an ECMAScript function object. A function environment may establish a new this binding. A function environment also captures the state necessary to support super method invocations.

    词法环境和环境记录的值纯粹是规范机制,不需要与 ECMAScript 实现中的任何特定 artefact 相对应。ECMAScript 程序无法直接访问或者操作这些值。

    Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.