13 ECMAScript 语言:表达式 (ECMAScript Language: Expressions)

13.1 标识符 (Identifiers)

语法 (Syntax)

IdentifierReference[Yield, Await] : Identifier [~Yield] yield [~Await] await BindingIdentifier[Yield, Await] : Identifier yield await LabelIdentifier[Yield, Await] : Identifier [~Yield] yield [~Await] await Identifier : IdentifierName but not ReservedWord Note

yieldawait 在语法中被允许作为 BindingIdentifier,稍后通过静态语义禁止,是为了阻止在如下情形发生自动分号插入:

let
await 0;

13.1.1 静态语义:早期错误 (Static Semantics: Early Errors)

BindingIdentifier : Identifier
  • 若 IsStrict(this production) 为 trueIdentifier 的 StringValue 为 "arguments""eval",则为语法错误。
IdentifierReference : yield BindingIdentifier : yield LabelIdentifier : yield
  • 若 IsStrict(this production) 为 true 则为语法错误。
IdentifierReference : await BindingIdentifier : await LabelIdentifier : await BindingIdentifier[Yield, Await] : yield
  • 若此产生式具有 [Yield] 参数则为语法错误。
BindingIdentifier[Yield, Await] : await
  • 若此产生式具有 [Await] 参数则为语法错误。
IdentifierReference[Yield, Await] : Identifier BindingIdentifier[Yield, Await] : Identifier LabelIdentifier[Yield, Await] : Identifier
  • 若此产生式具有 [Yield] 参数且 Identifier 的 StringValue 为 "yield",则为语法错误。
  • 若此产生式具有 [Await] 参数且 Identifier 的 StringValue 为 "await",则为语法错误。
Identifier : IdentifierName but not ReservedWord
  • 若 IsStrict(this phrase) 为 trueIdentifierName 的 StringValue 为 "implements""interface""let""package""private""protected""public""static""yield" 之一,则为语法错误。
  • 若句法语法目标符号ModuleIdentifierName 的 StringValue 为 "await",则为语法错误。
  • IdentifierName 的 StringValue 等于某 ReservedWord 的 StringValue 且该 ReservedWord 不是 yieldawait,则为语法错误。
Note

IdentifierName 的 StringValue 会规范化其中的任意 Unicode 转义序列,因此不能通过转义来写出与某 ReservedWord 码点序列相同的 Identifier

13.1.2 静态语义:StringValue

The syntax-directed operation 静态语义:StringValue takes no arguments and returns 一个 String. It is defined piecewise over the following productions:

IdentifierName :: IdentifierStart IdentifierName IdentifierPart
  1. idTextUnescapedIdentifierName 的 IdentifierCodePoints。
  2. 返回 CodePointsToString(idTextUnescaped)。
IdentifierReference : yield BindingIdentifier : yield LabelIdentifier : yield
  1. 返回 "yield"
IdentifierReference : await BindingIdentifier : await LabelIdentifier : await
  1. 返回 "await"
Identifier : IdentifierName but not ReservedWord
  1. 返回 IdentifierName 的 StringValue。
PrivateIdentifier :: # IdentifierName
  1. 返回 0x0023 (NUMBER SIGN) 与 IdentifierName 的 StringValue 的字符串连接。
ModuleExportName : StringLiteral
  1. 返回 StringLiteral 的 SV。

13.1.3 运行时语义:求值 (Runtime Semantics: Evaluation)

IdentifierReference : Identifier
  1. 返回 ? ResolveBinding(StringValue of Identifier)。
IdentifierReference : yield
  1. 返回 ? ResolveBinding("yield")。
IdentifierReference : await
  1. 返回 ? ResolveBinding("await")。
Note 1

对一个 IdentifierReference 求值的结果总是 Reference 类型的值。

Note 2

在非严格代码中,关键字 yield 可以作为标识符使用。对该 IdentifierReference 求值会解析 yield 的绑定,好像它是一个普通 Identifier。早期错误限制保证此求值只会出现在非严格代码。

13.2 基础表达式 (Primary Expression)

语法 (Syntax)

PrimaryExpression[Yield, Await] : this IdentifierReference[?Yield, ?Await] Literal ArrayLiteral[?Yield, ?Await] ObjectLiteral[?Yield, ?Await] FunctionExpression ClassExpression[?Yield, ?Await] GeneratorExpression AsyncFunctionExpression AsyncGeneratorExpression RegularExpressionLiteral TemplateLiteral[?Yield, ?Await, ~Tagged] CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await] CoverParenthesizedExpressionAndArrowParameterList[Yield, Await] : ( Expression[+In, ?Yield, ?Await] ) ( Expression[+In, ?Yield, ?Await] , ) ( ) ( ... BindingIdentifier[?Yield, ?Await] ) ( ... BindingPattern[?Yield, ?Await] ) ( Expression[+In, ?Yield, ?Await] , ... BindingIdentifier[?Yield, ?Await] ) ( Expression[+In, ?Yield, ?Await] , ... BindingPattern[?Yield, ?Await] )

补充语法 (Supplemental Syntax)

当处理产生式实例:
PrimaryExpression[Yield, Await] : CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await]
时,使用如下语法细化对 CoverParenthesizedExpressionAndArrowParameterList 的解释:

ParenthesizedExpression[Yield, Await] : ( Expression[+In, ?Yield, ?Await] )

13.2.1 this 关键字 (The this Keyword)

13.2.1.1 运行时语义:求值 (Runtime Semantics: Evaluation)

PrimaryExpression : this
  1. 返回 ? ResolveThisBinding()。

13.2.2 标识符引用 (Identifier Reference)

关于 IdentifierReference13.1

13.2.3 字面量 (Literals)

语法 (Syntax)

Literal : NullLiteral BooleanLiteral NumericLiteral StringLiteral

13.2.3.1 运行时语义:求值 (Runtime Semantics: Evaluation)

Literal : NullLiteral
  1. 返回 null
Literal : BooleanLiteral
  1. BooleanLiteral 为记号 false,返回 false
  2. BooleanLiteral 为记号 true,返回 true
Literal : NumericLiteral
  1. 返回 12.9.3 中定义的 NumericLiteral 的 NumericValue。
Literal : StringLiteral
  1. 返回 12.9.4.2 中定义的 StringLiteral 的 SV。

13.2.4 数组初始化器 (Array Initializer)

Note

ArrayLiteral 是描述数组初始化的表达式:一个由零个或多个表达式(各代表一个数组元素)构成的列表,置于方括号内。元素不必是字面量;数组初始化器每次求值都会重新求各元素表达式。

数组元素可在列表的开头、中间或结尾省略。若元素列表中的一个逗号未被 AssignmentExpression 前导(例如在开头或跟在另一个逗号后),则缺失的数组元素会计入 Array 的 length 并增加后续元素索引。被省略的数组元素未定义。若在数组末尾省略元素,该元素不计入 Array 的 length。

语法 (Syntax)

ArrayLiteral[Yield, Await] : [ Elisionopt ] [ ElementList[?Yield, ?Await] ] [ ElementList[?Yield, ?Await] , Elisionopt ] ElementList[Yield, Await] : Elisionopt AssignmentExpression[+In, ?Yield, ?Await] Elisionopt SpreadElement[?Yield, ?Await] ElementList[?Yield, ?Await] , Elisionopt AssignmentExpression[+In, ?Yield, ?Await] ElementList[?Yield, ?Await] , Elisionopt SpreadElement[?Yield, ?Await] Elision : , Elision , SpreadElement[Yield, Await] : ... AssignmentExpression[+In, ?Yield, ?Await]

13.2.4.1 运行时语义:ArrayAccumulation : 返回一个含整数的正常完成或一个突然完成

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

Elision : ,
  1. lennextIndex + 1。
  2. 执行 ? Set(array, "length", 𝔽(len), true)。
  3. 注:若 len 超过 232 - 1,上一步会抛出。
  4. 返回 len
Elision : Elision ,
  1. 返回 ? ArrayAccumulation of Elision with arguments array and (nextIndex + 1)。
ElementList : Elisionopt AssignmentExpression
  1. 若存在 Elision,则
    1. nextIndex 设为 ? ArrayAccumulation of Elision,参数 arraynextIndex
  2. initResult 为 ? Evaluation of AssignmentExpression
  3. initValue 为 ? GetValue(initResult)。
  4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), initValue)。
  5. 返回 nextIndex + 1。
ElementList : Elisionopt SpreadElement
  1. 若存在 Elision,则
    1. nextIndex 设为 ? ArrayAccumulation of Elision,参数 arraynextIndex
  2. 返回 ? ArrayAccumulation of SpreadElement,参数 arraynextIndex
ElementList : ElementList , Elisionopt AssignmentExpression
  1. nextIndex 设为 ? ArrayAccumulation of ElementList,参数 arraynextIndex
  2. 若存在 Elision,则
    1. nextIndex 设为 ? ArrayAccumulation of Elision,参数 arraynextIndex
  3. initResult 为 ? Evaluation of AssignmentExpression
  4. initValue 为 ? GetValue(initResult)。
  5. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), initValue)。
  6. 返回 nextIndex + 1。
ElementList : ElementList , Elisionopt SpreadElement
  1. nextIndex 设为 ? ArrayAccumulation of ElementList,参数 arraynextIndex
  2. 若存在 Elision,则
    1. nextIndex 设为 ? ArrayAccumulation of Elision,参数 arraynextIndex
  3. 返回 ? ArrayAccumulation of SpreadElement,参数 arraynextIndex
SpreadElement : ... AssignmentExpression
  1. spreadRef 为 ? Evaluation of AssignmentExpression
  2. spreadObj 为 ? GetValue(spreadRef)。
  3. iteratorRecord 为 ? GetIterator(spreadObj, sync)。
  4. 重复,
    1. next 为 ? IteratorStepValue(iteratorRecord)。
    2. nextdone,返回 nextIndex
    3. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), next)。
    4. nextIndex 设为 nextIndex + 1。
Note

使用 CreateDataPropertyOrThrow 以确保即使内置 Array 原型对象被修改(导致用 [[Set]] 无法创建新自有属性),数组自有属性仍能被定义。

13.2.4.2 运行时语义:求值 (Runtime Semantics: Evaluation)

ArrayLiteral : [ Elisionopt ]
  1. array 为 ! ArrayCreate(0)。
  2. 若存在 Elision,则
    1. 执行 ? ArrayAccumulation of Elision,参数 array 与 0。
  3. 返回 array
ArrayLiteral : [ ElementList ]
  1. array 为 ! ArrayCreate(0)。
  2. 执行 ? ArrayAccumulation of ElementList,参数 array 与 0。
  3. 返回 array
ArrayLiteral : [ ElementList , Elisionopt ]
  1. array 为 ! ArrayCreate(0)。
  2. nextIndex 为 ? ArrayAccumulation of ElementList,参数 array 与 0。
  3. 若存在 Elision,则
    1. 执行 ? ArrayAccumulation of Elision,参数 arraynextIndex
  4. 返回 array

13.2.5 对象初始化器 (Object Initializer)

Note 1

对象初始化器是描述对象初始化的表达式,其形式类似字面量。它是零个或多个属性键及其关联值的列表,置于花括号中。值不必是字面量;对象初始化器每次求值都会重新求各值。

语法 (Syntax)

ObjectLiteral[Yield, Await] : { } { PropertyDefinitionList[?Yield, ?Await] } { PropertyDefinitionList[?Yield, ?Await] , } PropertyDefinitionList[Yield, Await] : PropertyDefinition[?Yield, ?Await] PropertyDefinitionList[?Yield, ?Await] , PropertyDefinition[?Yield, ?Await] PropertyDefinition[Yield, Await] : IdentifierReference[?Yield, ?Await] CoverInitializedName[?Yield, ?Await] PropertyName[?Yield, ?Await] : AssignmentExpression[+In, ?Yield, ?Await] MethodDefinition[?Yield, ?Await] ... AssignmentExpression[+In, ?Yield, ?Await] PropertyName[Yield, Await] : LiteralPropertyName ComputedPropertyName[?Yield, ?Await] LiteralPropertyName : IdentifierName StringLiteral NumericLiteral ComputedPropertyName[Yield, Await] : [ AssignmentExpression[+In, ?Yield, ?Await] ] CoverInitializedName[Yield, Await] : IdentifierReference[?Yield, ?Await] Initializer[+In, ?Yield, ?Await] Initializer[In, Yield, Await] : = AssignmentExpression[?In, ?Yield, ?Await] Note 2 Note 3

在某些上下文中,ObjectLiteral 作为更受限次级语法的覆盖语法使用。CoverInitializedName 产生式的存在是为了完整覆盖这些次级语法。但在期望实际 ObjectLiteral 的正常上下文中使用该产生式会导致早期语法错误。

13.2.5.1 静态语义:早期错误 (Static Semantics: Early Errors)

PropertyDefinition : MethodDefinition

除了描述实际对象初始化器,ObjectLiteral 产生式也作为 ObjectAssignmentPattern覆盖语法,并可能作为 CoverParenthesizedExpressionAndArrowParameterList 一部分被识别。当 ObjectLiteral 出现在需要 ObjectAssignmentPattern 的上下文中时,下列早期错误规则适用;初始解析 CoverParenthesizedExpressionAndArrowParameterListCoverCallExpressionAndAsyncArrowHead 时也不适用。

PropertyDefinition : CoverInitializedName
  • 若有源码匹配该产生式则为语法错误。
Note 1

该产生式存在是为了让 ObjectLiteral 作为 ObjectAssignmentPattern覆盖语法;它不可能出现在实际对象初始化器中。

ObjectLiteral : { PropertyDefinitionList } { PropertyDefinitionList , } Note 2

PropertyNameList 返回的列表不包含通过 ComputedPropertyName 定义的属性名

13.2.5.2 静态语义:IsComputedPropertyKey

The syntax-directed operation 静态语义:IsComputedPropertyKey takes no arguments and returns Boolean. It is defined piecewise over the following productions:

PropertyName : LiteralPropertyName
  1. 返回 false
PropertyName : ComputedPropertyName
  1. 返回 true

13.2.5.3 静态语义:PropertyNameList

The syntax-directed operation 静态语义:PropertyNameList takes no arguments and returns 字符串列表 (a List of Strings). It is defined piecewise over the following productions:

PropertyDefinitionList : PropertyDefinition
  1. propNamePropertyDefinition 的 PropName。
  2. propNameempty,返回空新列表。
  3. 返回 « propName »。
PropertyDefinitionList : PropertyDefinitionList , PropertyDefinition
  1. listPropertyDefinitionList 的 PropertyNameList。
  2. propNamePropertyDefinition 的 PropName。
  3. propNameempty,返回 list
  4. 返回 list 与 « propName » 的列表连接。

13.2.5.4 运行时语义:求值 (Runtime Semantics: Evaluation)

ObjectLiteral : { }
  1. 返回 OrdinaryObjectCreate(%Object.prototype%)。
ObjectLiteral : { PropertyDefinitionList } { PropertyDefinitionList , }
  1. objOrdinaryObjectCreate(%Object.prototype%)。
  2. 执行 ? PropertyDefinitionEvaluation of PropertyDefinitionList,参数 obj
  3. 返回 obj
LiteralPropertyName : IdentifierName
  1. 返回 IdentifierName 的 StringValue。
LiteralPropertyName : StringLiteral
  1. 返回 StringLiteral 的 SV。
LiteralPropertyName : NumericLiteral
  1. nbrNumericLiteral 的 NumericValue。
  2. 返回 ! ToString(nbr)。
ComputedPropertyName : [ AssignmentExpression ]
  1. exprValue 为 ? Evaluation of AssignmentExpression
  2. propName 为 ? GetValue(exprValue)。
  3. 返回 ? ToPropertyKey(propName)。

13.2.5.5 运行时语义:PropertyDefinitionEvaluation : 返回含 unused 的正常完成或一个突然完成

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

PropertyDefinitionList : PropertyDefinitionList , PropertyDefinition
  1. 执行 ? PropertyDefinitionEvaluation of PropertyDefinitionList,参数 object
  2. 执行 ? PropertyDefinitionEvaluation of PropertyDefinition,参数 object
  3. 返回 unused
PropertyDefinition : ... AssignmentExpression
  1. exprValue 为 ? Evaluation of AssignmentExpression
  2. fromValue 为 ? GetValue(exprValue)。
  3. excludedNames 为新空列表。
  4. 执行 ? CopyDataProperties(object, fromValue, excludedNames)。
  5. 返回 unused
PropertyDefinition : IdentifierReference
  1. propNameIdentifierReference 的 StringValue。
  2. exprValue 为 ? Evaluation of IdentifierReference
  3. propValue 为 ? GetValue(exprValue)。
  4. 断言:object 是普通、可扩展且无不可配置属性的对象。
  5. 执行 ! CreateDataPropertyOrThrow(object, propName, propValue)。
  6. 返回 unused
PropertyDefinition : PropertyName : AssignmentExpression
  1. propKey 为 ? Evaluation of PropertyName
  2. 若此 PropertyDefinition 位于为 ParseJSON 求值的 Script 中(见 ParseJSON6 步骤),则
    1. isProtoSetterfalse
  3. 否则若 propKey"__proto__" 且 IsComputedPropertyKey(PropertyName) 为 false,则
    1. isProtoSettertrue
  4. 否则,
    1. isProtoSetterfalse
  5. 若 IsAnonymousFunctionDefinition(AssignmentExpression) 为 trueisProtoSetterfalse,则
    1. propValue 为 ? NamedEvaluation of AssignmentExpression,参数 propKey
  6. 否则,
    1. exprValueRef 为 ? Evaluation of AssignmentExpression
    2. propValue 为 ? GetValue(exprValueRef)。
  7. isProtoSettertrue,则
    1. propValue 是 ObjectpropValuenull,则
      1. 执行 ! object.[[SetPrototypeOf]](propValue)。
    2. 返回 unused
  8. 断言:object 是普通、可扩展且无不可配置属性的对象。
  9. 执行 ! CreateDataPropertyOrThrow(object, propKey, propValue)。
  10. 返回 unused
PropertyDefinition : MethodDefinition
  1. 执行 ? MethodDefinitionEvaluation of MethodDefinition,参数 objecttrue
  2. 返回 unused

13.2.6 定义函数的表达式 (Function Defining Expressions)

15.2 关于 PrimaryExpression : FunctionExpression

15.5 关于 PrimaryExpression : GeneratorExpression

15.7 关于 PrimaryExpression : ClassExpression

15.8 关于 PrimaryExpression : AsyncFunctionExpression

15.6 关于 PrimaryExpression : AsyncGeneratorExpression

13.2.7 正则表达式字面量 (Regular Expression Literals)

语法 (Syntax)

12.9.5

13.2.7.1 静态语义:早期错误 (Static Semantics: Early Errors)

PrimaryExpression : RegularExpressionLiteral

13.2.7.2 静态语义:IsValidRegularExpressionLiteral ( literal )

The abstract operation 静态语义:IsValidRegularExpressionLiteral takes argument literal (一个 RegularExpressionLiteral Parse Node) and returns Boolean. 判断参数是否为有效的正则表达式字面量。 It performs the following steps when called:

  1. flagsliteral 的 FlagText。
  2. flags 含有除 dgimsuvy 之外任意码点,或某码点出现多次,返回 false
  3. flagsu,设 utrue;否则 false
  4. flagsv,设 vtrue;否则 false
  5. patternTextliteral 的 BodyText。
  6. ufalsevfalse,则
    1. stringValue 为 CodePointsToString(patternText)。
    2. patternText 设为解释 stringValue 的每个 16 位元素为 Unicode BMP 码点后的码点序列(不执行 UTF-16 解码组合)。
  7. parseResult 为 ParsePattern(patternText, u, v)。
  8. parseResult 是 Parse Node,返回 true;否则返回 false

13.2.7.3 运行时语义:求值 (Runtime Semantics: Evaluation)

PrimaryExpression : RegularExpressionLiteral
  1. pattern 为 CodePointsToString(BodyText of RegularExpressionLiteral)。
  2. flags 为 CodePointsToString(FlagText of RegularExpressionLiteral)。
  3. 返回 ! RegExpCreate(pattern, flags)。

13.2.8 模板字面量 (Template Literals)

语法 (Syntax)

TemplateLiteral[Yield, Await, Tagged] : NoSubstitutionTemplate SubstitutionTemplate[?Yield, ?Await, ?Tagged] SubstitutionTemplate[Yield, Await, Tagged] : TemplateHead Expression[+In, ?Yield, ?Await] TemplateSpans[?Yield, ?Await, ?Tagged] TemplateSpans[Yield, Await, Tagged] : TemplateTail TemplateMiddleList[?Yield, ?Await, ?Tagged] TemplateTail TemplateMiddleList[Yield, Await, Tagged] : TemplateMiddle Expression[+In, ?Yield, ?Await] TemplateMiddleList[?Yield, ?Await, ?Tagged] TemplateMiddle Expression[+In, ?Yield, ?Await]

13.2.8.1 静态语义:早期错误 (Static Semantics: Early Errors)

TemplateLiteral[Yield, Await, Tagged] : NoSubstitutionTemplate TemplateLiteral[Yield, Await, Tagged] : SubstitutionTemplate[?Yield, ?Await, ?Tagged]
  • 若以 false 为参数的 TemplateLiteral 的 TemplateStrings 中元素数 ≥ 232,则为语法错误。
SubstitutionTemplate[Yield, Await, Tagged] : TemplateHead Expression[+In, ?Yield, ?Await] TemplateSpans[?Yield, ?Await, ?Tagged] TemplateSpans[Yield, Await, Tagged] : TemplateTail TemplateMiddleList[Yield, Await, Tagged] : TemplateMiddle Expression[+In, ?Yield, ?Await] TemplateMiddleList[?Yield, ?Await, ?Tagged] TemplateMiddle Expression[+In, ?Yield, ?Await]

13.2.8.2 静态语义:TemplateStrings : 一个由 String 或 undefined 组成的列表

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

TemplateLiteral : NoSubstitutionTemplate
  1. 返回 « TemplateString(NoSubstitutionTemplate, raw) »。
SubstitutionTemplate : TemplateHead Expression TemplateSpans
  1. head 为 « TemplateString(TemplateHead, raw) »。
  2. tailTemplateSpans 的 TemplateStrings,参数 raw
  3. 返回 headtail 的列表连接。
TemplateSpans : TemplateTail
  1. 返回 « TemplateString(TemplateTail, raw) »。
TemplateSpans : TemplateMiddleList TemplateTail
  1. middleTemplateMiddleList 的 TemplateStrings,参数 raw
  2. tail 为 « TemplateString(TemplateTail, raw) »。
  3. 返回 middletail 的列表连接。
TemplateMiddleList : TemplateMiddle Expression
  1. 返回 « TemplateString(TemplateMiddle, raw) »。
TemplateMiddleList : TemplateMiddleList TemplateMiddle Expression
  1. frontTemplateMiddleList 的 TemplateStrings,参数 raw
  2. last 为 « TemplateString(TemplateMiddle, raw) »。
  3. 返回 frontlast 的列表连接。

13.2.8.3 静态语义:TemplateString (Static Semantics: TemplateString) ( templateToken: 一个 NoSubstitutionTemplateTemplateHeadTemplateMiddleTemplateTail Parse Node, raw: Boolean, ): 一个 String 或 undefined

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. rawtrue,则
    1. stringtemplateToken 的 TRV。
  2. 否则,
    1. stringtemplateToken 的 TV。
  3. 返回 string
Note

rawfalsetemplateTokenNotEscapeSequence,该操作返回 undefined;其它情况返回 String。

13.2.8.4 GetTemplateObject ( templateLiteral )

The abstract operation GetTemplateObject takes argument templateLiteral (一个 Parse Node) and returns 一个 Array. It performs the following steps when called:

  1. realm当前 Realm Record
  2. templateRegistryrealm.[[TemplateMap]]
  3. templateRegistry 中每个元素 e
    1. e.[[Site]]templateLiteral 是同一 Parse Node,则
      1. 返回 e.[[Array]]
  4. rawStringstemplateLiteral 的 TemplateStrings,参数 true
  5. 断言:rawStrings 是 String 列表。
  6. cookedStringstemplateLiteral 的 TemplateStrings,参数 false
  7. countcookedStrings 元素数量。
  8. 断言:count ≤ 232 - 1。
  9. template 为 ! ArrayCreate(count)。
  10. rawObj 为 ! ArrayCreate(count)。
  11. index 为 0。
  12. index < count 重复:
    1. prop 为 ! ToString(𝔽(index))。
    2. cookedValuecookedStrings[index]。
    3. 执行 ! DefinePropertyOrThrow(template, prop, PropertyDescriptor { [[Value]]: cookedValue, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false })。
    4. rawValuerawStrings[index] 的 String 值。
    5. 执行 ! DefinePropertyOrThrow(rawObj, prop, PropertyDescriptor { [[Value]]: rawValue, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false })。
    6. index 设为 index + 1。
  13. 执行 ! SetIntegrityLevel(rawObj, frozen)。
  14. 执行 ! DefinePropertyOrThrow(template, "raw", PropertyDescriptor { [[Value]]: rawObj, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false })。
  15. 执行 ! SetIntegrityLevel(template, frozen)。
  16. 将记录 { [[Site]]: templateLiteral, [[Array]]: template } 附加到 realm.[[TemplateMap]]
  17. 返回 template
Note 1

创建模板对象不会导致突然完成。

Note 2

同一 realm 中程序代码里的每个 TemplateLiteral 关联一个用于求值标签模板(13.2.8.6)的唯一模板对象。模板对象被冻结;同一标签模板每次求值复用同一个模板对象。是首次求值惰性创建还是提前创建是实现细节,对 ECMAScript 代码不可观察。

Note 3

规范未来版本可能为模板对象定义额外不可枚举属性。

13.2.8.5 运行时语义:SubstitutionEvaluation

The syntax-directed operation 运行时语义:SubstitutionEvaluation takes no arguments and returns 返回含 ECMAScript 语言值列表的正常完成或一个突然完成. It is defined piecewise over the following productions:

TemplateSpans : TemplateTail
  1. 返回新的空列表。
TemplateSpans : TemplateMiddleList TemplateTail
  1. 返回 ? SubstitutionEvaluation of TemplateMiddleList
TemplateMiddleList : TemplateMiddle Expression
  1. subRef 为 ? Evaluation of Expression
  2. sub 为 ? GetValue(subRef)。
  3. 返回 « sub »。
TemplateMiddleList : TemplateMiddleList TemplateMiddle Expression
  1. preceding 为 ? SubstitutionEvaluation of TemplateMiddleList
  2. nextRef 为 ? Evaluation of Expression
  3. next 为 ? GetValue(nextRef)。
  4. 返回 preceding 与 « next » 的列表连接。

13.2.8.6 运行时语义:求值 (Runtime Semantics: Evaluation)

TemplateLiteral : NoSubstitutionTemplate
  1. 返回 12.9.6 中定义的 NoSubstitutionTemplate 的 TV。
SubstitutionTemplate : TemplateHead Expression TemplateSpans
  1. head12.9.6 中定义的 TemplateHead 的 TV。
  2. subRef 为 ? Evaluation of Expression
  3. sub 为 ? GetValue(subRef)。
  4. middle 为 ? ToString(sub)。
  5. tail 为 ? Evaluation of TemplateSpans
  6. 返回 headmiddletail 的字符串连接。
Note 1

Expression 值的字符串转换语义类似 String.prototype.concat 而非 + 运算符。

TemplateSpans : TemplateTail
  1. 返回 12.9.6 中定义的 TemplateTail 的 TV。
TemplateSpans : TemplateMiddleList TemplateTail
  1. head 为 ? Evaluation of TemplateMiddleList
  2. tail12.9.6 中定义的 TemplateTail 的 TV。
  3. 返回 headtail 的字符串连接。
TemplateMiddleList : TemplateMiddle Expression
  1. head12.9.6 中定义的 TemplateMiddle 的 TV。
  2. subRef 为 ? Evaluation of Expression
  3. sub 为 ? GetValue(subRef)。
  4. middle 为 ? ToString(sub)。
  5. 返回 headmiddle 的字符串连接。
Note 2

Expression 值的字符串转换语义类似 String.prototype.concat 而非 + 运算符。

TemplateMiddleList : TemplateMiddleList TemplateMiddle Expression
  1. rest 为 ? Evaluation of TemplateMiddleList
  2. middle12.9.6 中定义的 TemplateMiddle 的 TV。
  3. subRef 为 ? Evaluation of Expression
  4. sub 为 ? GetValue(subRef)。
  5. last 为 ? ToString(sub)。
  6. 返回 restmiddlelast 的字符串连接。
Note 3

Expression 值的字符串转换语义类似 String.prototype.concat 而非 + 运算符。

13.2.9 分组运算符 (The Grouping Operator)

13.2.9.1 静态语义:早期错误 (Static Semantics: Early Errors)

PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList

13.2.9.2 运行时语义:求值 (Runtime Semantics: Evaluation)

PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList
  1. exprCoverParenthesizedExpressionAndArrowParameterList覆盖ParenthesizedExpression
  2. 返回 ? Evaluation of expr
ParenthesizedExpression : ( Expression )
  1. 返回 ? Evaluation of Expression。其结果可能是 Reference 类型。
Note

该算法不对 Expression 的 Evaluation 结果执行 GetValue。其主要动机是允许对括号表达式应用 deletetypeof 等运算符。

13.3 左值表达式 (Left-Hand-Side Expressions)

语法 (Syntax)

MemberExpression[Yield, Await] : PrimaryExpression[?Yield, ?Await] MemberExpression[?Yield, ?Await] [ Expression[+In, ?Yield, ?Await] ] MemberExpression[?Yield, ?Await] . IdentifierName MemberExpression[?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged] SuperProperty[?Yield, ?Await] MetaProperty new MemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await] MemberExpression[?Yield, ?Await] . PrivateIdentifier SuperProperty[Yield, Await] : super [ Expression[+In, ?Yield, ?Await] ] super . IdentifierName MetaProperty : NewTarget ImportMeta NewTarget : new . target ImportMeta : import . meta NewExpression[Yield, Await] : MemberExpression[?Yield, ?Await] new NewExpression[?Yield, ?Await] CallExpression[Yield, Await] : CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] SuperCall[?Yield, ?Await] ImportCall[?Yield, ?Await] CallExpression[?Yield, ?Await] Arguments[?Yield, ?Await] CallExpression[?Yield, ?Await] [ Expression[+In, ?Yield, ?Await] ] CallExpression[?Yield, ?Await] . IdentifierName CallExpression[?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged] CallExpression[?Yield, ?Await] . PrivateIdentifier SuperCall[Yield, Await] : super Arguments[?Yield, ?Await] ImportCall[Yield, Await] : import ( AssignmentExpression[+In, ?Yield, ?Await] ,opt ) import ( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt ) Arguments[Yield, Await] : ( ) ( ArgumentList[?Yield, ?Await] ) ( ArgumentList[?Yield, ?Await] , ) ArgumentList[Yield, Await] : AssignmentExpression[+In, ?Yield, ?Await] ... AssignmentExpression[+In, ?Yield, ?Await] ArgumentList[?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ArgumentList[?Yield, ?Await] , ... AssignmentExpression[+In, ?Yield, ?Await] OptionalExpression[Yield, Await] : MemberExpression[?Yield, ?Await] OptionalChain[?Yield, ?Await] CallExpression[?Yield, ?Await] OptionalChain[?Yield, ?Await] OptionalExpression[?Yield, ?Await] OptionalChain[?Yield, ?Await] OptionalChain[Yield, Await] : ?. Arguments[?Yield, ?Await] ?. [ Expression[+In, ?Yield, ?Await] ] ?. IdentifierName ?. TemplateLiteral[?Yield, ?Await, +Tagged] ?. PrivateIdentifier OptionalChain[?Yield, ?Await] Arguments[?Yield, ?Await] OptionalChain[?Yield, ?Await] [ Expression[+In, ?Yield, ?Await] ] OptionalChain[?Yield, ?Await] . IdentifierName OptionalChain[?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged] OptionalChain[?Yield, ?Await] . PrivateIdentifier LeftHandSideExpression[Yield, Await] : NewExpression[?Yield, ?Await] CallExpression[?Yield, ?Await] OptionalExpression[?Yield, ?Await]

补充语法 (Supplemental Syntax)

当处理产生式实例:
CallExpression : CoverCallExpressionAndAsyncArrowHead
时,使用下列语法细化对 CoverCallExpressionAndAsyncArrowHead 的解释:

CallMemberExpression[Yield, Await] : MemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await]

13.3.1 静态语义 (Static Semantics)

13.3.1.1 静态语义:早期错误 (Static Semantics: Early Errors)

OptionalChain : ?. TemplateLiteral OptionalChain TemplateLiteral
  • 若有源码匹配该产生式则为语法错误。
Note

该产生式存在是为了阻止自动分号插入规则(12.10)应用于以下代码:

a?.b
`c`

使其被解释为两个合法语句。其目的在于与没有可选链的类似代码保持一致:

a.b
`c`

后者是一个合法语句,且自动分号插入不适用。

ImportMeta : import . meta

13.3.2 属性访问器 (Property Accessors)

Note

属性按名称访问,可以使用点号表示法:

也可以使用方括号表示法:

点号表示法可以通过以下句法转换来解释:

其行为与

MemberExpression [ <identifier-name-string> ]

相同;类似地

其行为与

CallExpression [ <identifier-name-string> ]

相同,其中 <identifier-name-string> 为 IdentifierName 的 StringValue。

13.3.2.1 运行时语义:求值 (Runtime Semantics: Evaluation)

MemberExpression : MemberExpression [ Expression ]
  1. baseReference 为 ? Evaluation of MemberExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. strict 为 IsStrict(this MemberExpression)。
  4. 返回 ? EvaluatePropertyAccessWithExpressionKey(baseValue, Expression, strict)。
MemberExpression : MemberExpression . IdentifierName
  1. baseReference 为 ? Evaluation of MemberExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. strict 为 IsStrict(this MemberExpression)。
  4. 返回 EvaluatePropertyAccessWithIdentifierKey(baseValue, IdentifierName, strict)。
MemberExpression : MemberExpression . PrivateIdentifier
  1. baseReference 为 ? Evaluation of MemberExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. fieldNameStringPrivateIdentifier 的 StringValue。
  4. 返回 MakePrivateReference(baseValue, fieldNameString)。
CallExpression : CallExpression [ Expression ]
  1. baseReference 为 ? Evaluation of CallExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. strict 为 IsStrict(this CallExpression)。
  4. 返回 ? EvaluatePropertyAccessWithExpressionKey(baseValue, Expression, strict)。
CallExpression : CallExpression . IdentifierName
  1. baseReference 为 ? Evaluation of CallExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. strict 为 IsStrict(this CallExpression)。
  4. 返回 EvaluatePropertyAccessWithIdentifierKey(baseValue, IdentifierName, strict)。
CallExpression : CallExpression . PrivateIdentifier
  1. baseReference 为 ? Evaluation of CallExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. fieldNameStringPrivateIdentifier 的 StringValue。
  4. 返回 MakePrivateReference(baseValue, fieldNameString)。

13.3.3 EvaluatePropertyAccessWithExpressionKey ( baseValue, expression, strict )

The abstract operation EvaluatePropertyAccessWithExpressionKey takes arguments baseValue (一个 ECMAScript 语言值), expression (一个 Expression Parse Node), and strict (一个 Boolean) and returns 返回一个含 Reference Record正常完成或一个突然完成. It performs the following steps when called:

  1. propertyNameReference 为 ? Evaluation of expression
  2. propertyNameValue 为 ? GetValue(propertyNameReference)。
  3. 注:在大多数情况下,会在此步骤后立即对 propertyNameValue 执行 ToPropertyKey。但在 a[b] = c 的情形下,会延迟到 c 求值之后。
  4. 返回 Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyNameValue, [[Strict]]: strict, [[ThisValue]]: empty }。

13.3.4 EvaluatePropertyAccessWithIdentifierKey ( baseValue, identifierName, strict )

The abstract operation EvaluatePropertyAccessWithIdentifierKey takes arguments baseValue (一个 ECMAScript 语言值), identifierName (一个 IdentifierName Parse Node), and strict (一个 Boolean) and returns 一个 Reference Record. It performs the following steps when called:

  1. propertyNameStringidentifierName 的 StringValue。
  2. 返回 Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyNameString, [[Strict]]: strict, [[ThisValue]]: empty }。

13.3.5 new 运算符 (The new Operator)

13.3.5.1 运行时语义:求值 (Runtime Semantics: Evaluation)

NewExpression : new NewExpression
  1. 返回 ? EvaluateNew(NewExpression, empty)。
MemberExpression : new MemberExpression Arguments
  1. 返回 ? EvaluateNew(MemberExpression, Arguments)。

13.3.5.1.1 EvaluateNew ( constructExpr, arguments )

The abstract operation EvaluateNew takes arguments constructExpr (一个 NewExpression Parse Node 或 MemberExpression Parse Node) and arguments (empty 或一个 Arguments Parse Node) and returns 返回一个含 ECMAScript 语言值正常完成或一个突然完成. It performs the following steps when called:

  1. ref 为 ? Evaluation of constructExpr
  2. constructor 为 ? GetValue(ref)。
  3. argumentsempty,则
    1. argList 为新空列表。
  4. 否则,
    1. argList 为 ? ArgumentListEvaluation of arguments
  5. IsConstructor(constructor) 为 false,抛出 TypeError 异常。
  6. 返回 ? Construct(constructor, argList)。

13.3.6 函数调用 (Function Calls)

13.3.6.1 运行时语义:求值 (Runtime Semantics: Evaluation)

CallExpression : CoverCallExpressionAndAsyncArrowHead
  1. exprCoverCallExpressionAndAsyncArrowHead 覆盖CallMemberExpression
  2. memberExprexprMemberExpression
  3. argumentsexprArguments
  4. ref 为 ? Evaluation of memberExpr
  5. func 为 ? GetValue(ref)。
  6. refReference RecordIsPropertyReference(ref) 为 false,且 ref.[[ReferencedName]]"eval",则
    1. SameValue(func, %eval%) 为 true,则
      1. argList 为 ? ArgumentListEvaluation of arguments
      2. argList 无元素,返回 undefined
      3. evalArgargList 的第一个元素。
      4. 若 IsStrict(this CallExpression) 为 true,令 strictCallertrue;否则为 false
      5. 返回 ? PerformEval(evalArg, strictCaller, true)。
  7. thisCall 为 this CallExpression
  8. tailCall 为 IsInTailPosition(thisCall)。
  9. 返回 ? EvaluateCall(func, ref, arguments, tailCall)。

执行步骤 6.a.vCallExpression 求值是一次直接 eval (direct eval)

CallExpression : CallExpression Arguments
  1. ref 为 ? Evaluation of CallExpression
  2. func 为 ? GetValue(ref)。
  3. thisCall 为 this CallExpression
  4. tailCall 为 IsInTailPosition(thisCall)。
  5. 返回 ? EvaluateCall(func, ref, Arguments, tailCall)。

13.3.6.2 EvaluateCall ( func, ref, arguments, tailPosition )

The abstract operation EvaluateCall takes arguments func (一个 ECMAScript 语言值), ref (一个 ECMAScript 语言值Reference Record), arguments (一个 Parse Node), and tailPosition (一个 Boolean) and returns 返回一个含 ECMAScript 语言值正常完成或一个突然完成. It performs the following steps when called:

  1. refReference Record,则
    1. IsPropertyReference(ref) 为 true,则
      1. thisValueGetThisValue(ref)。
    2. 否则,
      1. refEnvref.[[Base]]
      2. 断言:refEnv 是一个 Environment Record
      3. thisValuerefEnv.WithBaseObject()。
  2. 否则,
    1. thisValueundefined
  3. argList 为 ? ArgumentListEvaluation of arguments
  4. func 不是 Object,抛出 TypeError 异常。
  5. IsCallable(func) 为 false,抛出 TypeError 异常。
  6. tailPositiontrue,执行 PrepareForTailCall()。
  7. 返回 ? Call(func, thisValue, argList)。

13.3.7 super 关键字 (The super Keyword)

13.3.7.1 运行时语义:求值 (Runtime Semantics: Evaluation)

SuperProperty : super [ Expression ]
  1. envGetThisEnvironment()。
  2. actualThis 为 ? env.GetThisBinding()。
  3. propertyNameReference 为 ? Evaluation of Expression
  4. propertyNameValue 为 ? GetValue(propertyNameReference)。
  5. strict 为 IsStrict(this SuperProperty)。
  6. 注:在大多数情况下,会在此步骤后立即对 propertyNameValue 执行 ToPropertyKey。但在 super[b] = c 的情形下,会延迟到 c 求值后。
  7. 返回 MakeSuperPropertyReference(actualThis, propertyNameValue, strict)。
SuperProperty : super . IdentifierName
  1. envGetThisEnvironment()。
  2. actualThis 为 ? env.GetThisBinding()。
  3. propertyKeyIdentifierName 的 StringValue。
  4. strict 为 IsStrict(this SuperProperty)。
  5. 返回 MakeSuperPropertyReference(actualThis, propertyKey, strict)。
SuperCall : super Arguments
  1. newTargetGetNewTarget()。
  2. 断言:newTarget 是一个构造函数
  3. funcGetSuperConstructor()。
  4. argList 为 ? ArgumentListEvaluation of Arguments
  5. IsConstructor(func) 为 false,抛出 TypeError 异常。
  6. result 为 ? Construct(func, argList, newTarget)。
  7. thisERGetThisEnvironment()。
  8. 断言:thisER 是一个 Function Environment Record
  9. 执行 ? BindThisValue(thisER, result)。
  10. FthisER.[[FunctionObject]]
  11. 断言:F 是一个 ECMAScript 函数对象
  12. 执行 ? InitializeInstanceElements(result, F)。
  13. 返回 result

13.3.7.2 GetSuperConstructor ( )

The abstract operation GetSuperConstructor takes no arguments and returns 一个 ECMAScript 语言值. It performs the following steps when called:

  1. envRecGetThisEnvironment()。
  2. 断言:envRec 是一个 Function Environment Record
  3. activeFunctionenvRec.[[FunctionObject]]
  4. 断言:activeFunction 是一个 ECMAScript 函数对象
  5. superConstructor 为 ! activeFunction.[[GetPrototypeOf]]()。
  6. 返回 superConstructor

13.3.7.3 MakeSuperPropertyReference ( actualThis, propertyKey, strict )

The abstract operation MakeSuperPropertyReference takes arguments actualThis (一个 ECMAScript 语言值), propertyKey (一个 ECMAScript 语言值), and strict (一个 Boolean) and returns 一个 Super Reference Record. It performs the following steps when called:

  1. envGetThisEnvironment()。
  2. 断言:env.HasSuperBinding() 为 true
  3. 断言:env 是一个 Function Environment Record
  4. baseValueGetSuperBase(env)。
  5. 返回 Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }。

13.3.8 参数列表 (Argument Lists)

Note

对参数列表的求值会产生一个值的 List

13.3.8.1 运行时语义:ArgumentListEvaluation

The syntax-directed operation 运行时语义:ArgumentListEvaluation takes no arguments and returns 返回一个含 ECMAScript 语言值列表的正常完成或一个突然完成. It is defined piecewise over the following productions:

Arguments : ( )
  1. 返回一个新的空列表。
ArgumentList : AssignmentExpression
  1. ref 为 ? Evaluation of AssignmentExpression
  2. arg 为 ? GetValue(ref)。
  3. 返回 « arg »。
ArgumentList : ... AssignmentExpression
  1. list 为新空列表。
  2. spreadRef 为 ? Evaluation of AssignmentExpression
  3. spreadObj 为 ? GetValue(spreadRef)。
  4. iteratorRecord 为 ? GetIterator(spreadObj, sync)。
  5. 重复,
    1. next 为 ? IteratorStepValue(iteratorRecord)。
    2. nextdone,返回 list
    3. next 追加到 list
ArgumentList : ArgumentList , AssignmentExpression
  1. precedingArgs 为 ? ArgumentListEvaluation of ArgumentList
  2. ref 为 ? Evaluation of AssignmentExpression
  3. arg 为 ? GetValue(ref)。
  4. 返回 precedingArgs 与 « arg » 的列表连接。
ArgumentList : ArgumentList , ... AssignmentExpression
  1. precedingArgs 为 ? ArgumentListEvaluation of ArgumentList
  2. spreadRef 为 ? Evaluation of AssignmentExpression
  3. iteratorRecord 为 ? GetIterator(? GetValue(spreadRef), sync)。
  4. 重复,
    1. next 为 ? IteratorStepValue(iteratorRecord)。
    2. nextdone,返回 precedingArgs
    3. next 追加到 precedingArgs
TemplateLiteral : NoSubstitutionTemplate
  1. templateLiteral 为 this TemplateLiteral
  2. siteObjGetTemplateObject(templateLiteral)。
  3. 返回 « siteObj »。
TemplateLiteral : SubstitutionTemplate
  1. templateLiteral 为 this TemplateLiteral
  2. siteObjGetTemplateObject(templateLiteral)。
  3. remaining 为 ? ArgumentListEvaluation of SubstitutionTemplate
  4. 返回 « siteObj » 与 remaining 的列表连接。
SubstitutionTemplate : TemplateHead Expression TemplateSpans
  1. firstSubRef 为 ? Evaluation of Expression
  2. firstSub 为 ? GetValue(firstSubRef)。
  3. restSub 为 ? SubstitutionEvaluation of TemplateSpans
  4. 断言:restSub 是一个可为空的列表。
  5. 返回 « firstSub » 与 restSub 的列表连接。

13.3.9 可选链 (Optional Chains)

Note
可选链是一个或多个属性访问与函数调用组成的链,其首个访问以 ?. 记号开始。

13.3.9.1 运行时语义:求值 (Runtime Semantics: Evaluation)

OptionalExpression : MemberExpression OptionalChain
  1. baseReference 为 ? Evaluation of MemberExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. baseValueundefinednull,则
    1. 返回 undefined
  4. 返回 ? ChainEvaluation of OptionalChain,参数 baseValuebaseReference
OptionalExpression : CallExpression OptionalChain
  1. baseReference 为 ? Evaluation of CallExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. baseValueundefinednull,则
    1. 返回 undefined
  4. 返回 ? ChainEvaluation of OptionalChain,参数 baseValuebaseReference
OptionalExpression : OptionalExpression OptionalChain
  1. baseReference 为 ? Evaluation of OptionalExpression
  2. baseValue 为 ? GetValue(baseReference)。
  3. baseValueundefinednull,则
    1. 返回 undefined
  4. 返回 ? ChainEvaluation of OptionalChain,参数 baseValuebaseReference

13.3.9.2 运行时语义:ChainEvaluation : 返回一个含 ECMAScript 语言值或 Reference Record 的正常完成,或一个突然完成

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

OptionalChain : ?. Arguments
  1. thisChain 为 this OptionalChain
  2. tailCall 为 IsInTailPosition(thisChain)。
  3. 返回 ? EvaluateCall(baseValue, baseReference, Arguments, tailCall)。
OptionalChain : ?. [ Expression ]
  1. strict 为 IsStrict(this OptionalChain)。
  2. 返回 ? EvaluatePropertyAccessWithExpressionKey(baseValue, Expression, strict)。
OptionalChain : ?. IdentifierName
  1. strict 为 IsStrict(this OptionalChain)。
  2. 返回 EvaluatePropertyAccessWithIdentifierKey(baseValue, IdentifierName, strict)。
OptionalChain : ?. PrivateIdentifier
  1. fieldNameStringPrivateIdentifier 的 StringValue。
  2. 返回 MakePrivateReference(baseValue, fieldNameString)。
OptionalChain : OptionalChain Arguments
  1. optionalChainOptionalChain
  2. newReference 为 ? ChainEvaluation of optionalChain,参数 baseValuebaseReference
  3. newValue 为 ? GetValue(newReference)。
  4. thisChain 为 this OptionalChain
  5. tailCall 为 IsInTailPosition(thisChain)。
  6. 返回 ? EvaluateCall(newValue, newReference, Arguments, tailCall)。
OptionalChain : OptionalChain [ Expression ]
  1. optionalChainOptionalChain
  2. newReference 为 ? ChainEvaluation of optionalChain,参数 baseValuebaseReference
  3. newValue 为 ? GetValue(newReference)。
  4. strict 为 IsStrict(this OptionalChain)。
  5. 返回 ? EvaluatePropertyAccessWithExpressionKey(newValue, Expression, strict)。
OptionalChain : OptionalChain . IdentifierName
  1. optionalChainOptionalChain
  2. newReference 为 ? ChainEvaluation of optionalChain,参数 baseValuebaseReference
  3. newValue 为 ? GetValue(newReference)。
  4. strict 为 IsStrict(this OptionalChain)。
  5. 返回 EvaluatePropertyAccessWithIdentifierKey(newValue, IdentifierName, strict)。
OptionalChain : OptionalChain . PrivateIdentifier
  1. optionalChainOptionalChain
  2. newReference 为 ? ChainEvaluation of optionalChain,参数 baseValuebaseReference
  3. newValue 为 ? GetValue(newReference)。
  4. fieldNameStringPrivateIdentifier 的 StringValue。
  5. 返回 MakePrivateReference(newValue, fieldNameString)。

13.3.10 Import 调用 (Import Calls)

13.3.10.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ImportCall : import ( AssignmentExpression ,opt )
  1. 返回 ? EvaluateImportCall(AssignmentExpression)。
ImportCall : import ( AssignmentExpression , AssignmentExpression ,opt )
  1. 返回 ? EvaluateImportCall(第一个 AssignmentExpression, 第二个 AssignmentExpression)。

13.3.10.2 EvaluateImportCall ( specifierExpression: 一个 Parse Node, 可选 optionsExpression: 一个 Parse Node, ): 返回一个含 Promise 的正常完成或一个突然完成

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. referrerGetActiveScriptOrModule()。
  2. referrernull,将 referrer 设为当前 Realm Record
  3. specifierRef 为 ? Evaluation of specifierExpression
  4. specifier 为 ? GetValue(specifierRef)。
  5. 若存在 optionsExpression,则
    1. optionsRef 为 ? Evaluation of optionsExpression
    2. options 为 ? GetValue(optionsRef)。
  6. 否则,
    1. optionsundefined
  7. promiseCapability 为 ! NewPromiseCapability(%Promise%)。
  8. specifierStringCompletion(ToString(specifier))。
  9. IfAbruptRejectPromise(specifierString, promiseCapability)。
  10. attributes 为新空列表。
  11. options 不为 undefined,则
    1. options 不是 Object,则
      1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « 新建的 TypeError 对象 »)。
      2. 返回 promiseCapability.[[Promise]]
    2. attributesObjCompletion(Get(options, "with"))。
    3. IfAbruptRejectPromise(attributesObj, promiseCapability)。
    4. attributesObj 不为 undefined,则
      1. attributesObj 不是 Object,则
        1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « 新建的 TypeError 对象 »)。
        2. 返回 promiseCapability.[[Promise]]
      2. entriesCompletion(EnumerableOwnProperties(attributesObj, key+value))。
      3. IfAbruptRejectPromise(entries, promiseCapability)。
      4. entries 中每个元素 entry
        1. key 为 ! Get(entry, "0")。
        2. value 为 ! Get(entry, "1")。
        3. key 是 String,则
          1. value 不是 String,则
            1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « 新建的 TypeError 对象 »)。
            2. 返回 promiseCapability.[[Promise]]
          2. ImportAttribute Record { [[Key]]: key, [[Value]]: value } 追加到 attributes
    5. AllImportAttributesSupported(attributes) 为 false,则
      1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « 新建的 TypeError 对象 »)。
      2. 返回 promiseCapability.[[Promise]]
    6. [[Key]] 字段字典序(将字段值视为 UTF-16 代码单元序列)对 attributes 排序。注:排序唯一可观察之处是宿主被禁止基于枚举顺序改变行为。
  12. moduleRequest 为新 ModuleRequest Record { [[Specifier]]: specifierString, [[Attributes]]: attributes }。
  13. 执行 HostLoadImportedModule(referrer, moduleRequest, empty, promiseCapability)。
  14. 返回 promiseCapability.[[Promise]]

13.3.10.3 ContinueDynamicImport ( promiseCapability, moduleCompletion )

The abstract operation ContinueDynamicImport takes arguments promiseCapability (一个 PromiseCapability Record) and moduleCompletion (一个含 Module Record正常完成或一个 throw completion) and returns unused. 它完成最初由 import() 调用开始的动态导入过程,适当地 resolve 或 reject 该调用返回的 promise。 It performs the following steps when called:

  1. moduleCompletion 是突然完成,则
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « moduleCompletion.[[Value]] »)。
    2. 返回 unused
  2. modulemoduleCompletion.[[Value]]
  3. loadPromisemodule.LoadRequestedModules()。
  4. rejectedClosure 为带参数 (reason)、捕获 promiseCapability 的新抽象闭包,调用时执行:
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « reason »)。
    2. 返回 NormalCompletion(undefined)。
  5. onRejectedCreateBuiltinFunction(rejectedClosure, 1, "", « »)。
  6. linkAndEvaluateClosure 为无参数、捕获 modulepromiseCapabilityonRejected 的新抽象闭包,调用时执行:
    1. linkCompletion(module.Link())。
    2. link 是突然完成,则
      1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « link.[[Value]] »)。
      2. 返回 NormalCompletion(undefined)。
    3. evaluatePromisemodule.Evaluate()。
    4. fulfilledClosure 为无参数、捕获 modulepromiseCapability 的新抽象闭包,调用时执行:
      1. namespaceGetModuleNamespace(module)。
      2. 执行 ! Call(promiseCapability.[[Resolve]], undefined, « namespace »)。
      3. 返回 NormalCompletion(undefined)。
    5. onFulfilledCreateBuiltinFunction(fulfilledClosure, 0, "", « »)。
    6. 执行 PerformPromiseThen(evaluatePromise, onFulfilled, onRejected)。
    7. 返回 unused
  7. linkAndEvaluateCreateBuiltinFunction(linkAndEvaluateClosure, 0, "", « »)。
  8. 执行 PerformPromiseThen(loadPromise, linkAndEvaluate, onRejected)。
  9. 返回 unused

13.3.11 带标签模板 (Tagged Templates)

Note

带标签模板是一个函数调用,其调用参数来源于 TemplateLiteral13.2.8)。实际参数包含一个模板对象(13.2.8.4)以及对 TemplateLiteral 内嵌表达式求值得到的值。

13.3.11.1 运行时语义:求值 (Runtime Semantics: Evaluation)

MemberExpression : MemberExpression TemplateLiteral
  1. tagRef 为 ? Evaluation of MemberExpression
  2. tagFunc 为 ? GetValue(tagRef)。
  3. thisCall 为 this MemberExpression
  4. tailCall 为 IsInTailPosition(thisCall)。
  5. 返回 ? EvaluateCall(tagFunc, tagRef, TemplateLiteral, tailCall)。
CallExpression : CallExpression TemplateLiteral
  1. tagRef 为 ? Evaluation of CallExpression
  2. tagFunc 为 ? GetValue(tagRef)。
  3. thisCall 为 this CallExpression
  4. tailCall 为 IsInTailPosition(thisCall)。
  5. 返回 ? EvaluateCall(tagFunc, tagRef, TemplateLiteral, tailCall)。

13.3.12 元属性 (Meta Properties)

13.3.12.1 运行时语义:求值 (Runtime Semantics: Evaluation)

NewTarget : new . target
  1. 返回 GetNewTarget()。
ImportMeta : import . meta
  1. moduleGetActiveScriptOrModule()。
  2. 断言:module 是一个 Source Text Module Record
  3. importMetamodule.[[ImportMeta]]
  4. importMetaempty,则
    1. importMeta 设为 OrdinaryObjectCreate(null)。
    2. importMetaValuesHostGetImportMetaProperties(module)。
    3. importMetaValues 中每个 Record { [[Key]], [[Value]] } p
      1. 执行 ! CreateDataPropertyOrThrow(importMeta, p.[[Key]], p.[[Value]])。
    4. 执行 HostFinalizeImportMeta(importMeta, module)。
    5. module.[[ImportMeta]] 设为 importMeta
    6. 返回 importMeta
  5. 否则,
    1. 断言:importMeta 是一个 Object。
    2. 返回 importMeta

13.3.12.1.1 HostGetImportMetaProperties ( moduleRecord )

The host-defined abstract operation HostGetImportMetaProperties takes argument moduleRecord (一个 Module Record) and returns 一个具有 [[Key]]属性键)与 [[Value]]ECMAScript 语言值)字段的 Record 列表. 允许宿主import.meta 返回的对象提供属性键与值。

HostGetImportMetaProperties 的默认实现是返回一个新的空列表。

13.3.12.1.2 HostFinalizeImportMeta ( importMeta, moduleRecord )

The host-defined abstract operation HostFinalizeImportMeta takes arguments importMeta (一个 Object) and moduleRecord (一个 Module Record) and returns unused. 允许宿主执行额外操作以准备将 import.meta 返回的对象暴露给 ECMAScript 代码。

大多数宿主仅需定义 HostGetImportMetaProperties,并保持 HostFinalizeImportMeta 的默认行为。但 HostFinalizeImportMeta 为需要在对象暴露前直接操作该对象的宿主提供“逃生口”。

HostFinalizeImportMeta 的默认实现是返回 unused

13.4 更新表达式 (Update Expressions)

语法 (Syntax)

UpdateExpression[Yield, Await] : LeftHandSideExpression[?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] [no LineTerminator here] ++ LeftHandSideExpression[?Yield, ?Await] [no LineTerminator here] -- ++ UnaryExpression[?Yield, ?Await] -- UnaryExpression[?Yield, ?Await]

13.4.1 静态语义:早期错误 (Static Semantics: Early Errors)

UpdateExpression : LeftHandSideExpression ++ LeftHandSideExpression -- UpdateExpression : ++ UnaryExpression -- UnaryExpression
  • UnaryExpression 的 AssignmentTargetType 为 invalid,则属于早期语法错误。

13.4.2 后缀自增运算符 (Postfix Increment Operator)

13.4.2.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UpdateExpression : LeftHandSideExpression ++
  1. lhs 为 ? Evaluation of LeftHandSideExpression
  2. LeftHandSideExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
  3. oldValue 为 ? ToNumeric(? GetValue(lhs))。
  4. oldValue 是 Number,则
    1. newValueNumber::add(oldValue, 1𝔽)。
  5. 否则,
    1. 断言:oldValue 为 BigInt。
    2. newValueBigInt::add(oldValue, 1)。
  6. 执行 ? PutValue(lhs, newValue)。
  7. 返回 oldValue

13.4.3 后缀自减运算符 (Postfix Decrement Operator)

13.4.3.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UpdateExpression : LeftHandSideExpression --
  1. lhs 为 ? Evaluation of LeftHandSideExpression
  2. LeftHandSideExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
  3. oldValue 为 ? ToNumeric(? GetValue(lhs))。
  4. oldValue 是 Number,则
    1. newValueNumber::subtract(oldValue, 1𝔽)。
  5. 否则,
    1. 断言:oldValue 为 BigInt。
    2. newValueBigInt::subtract(oldValue, 1)。
  6. 执行 ? PutValue(lhs, newValue)。
  7. 返回 oldValue

13.4.4 前缀自增运算符 (Prefix Increment Operator)

13.4.4.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UpdateExpression : ++ UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. UnaryExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
  3. oldValue 为 ? ToNumeric(? GetValue(expr))。
  4. oldValue 是 Number,则
    1. newValueNumber::add(oldValue, 1𝔽)。
  5. 否则,
    1. 断言:oldValue 为 BigInt。
    2. newValueBigInt::add(oldValue, 1)。
  6. 执行 ? PutValue(expr, newValue)。
  7. 返回 newValue

13.4.5 前缀自减运算符 (Prefix Decrement Operator)

13.4.5.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UpdateExpression : -- UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. UnaryExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
  3. oldValue 为 ? ToNumeric(? GetValue(expr))。
  4. oldValue 是 Number,则
    1. newValueNumber::subtract(oldValue, 1𝔽)。
  5. 否则,
    1. 断言:oldValue 为 BigInt。
    2. newValueBigInt::subtract(oldValue, 1)。
  6. 执行 ? PutValue(expr, newValue)。
  7. 返回 newValue

13.5 一元运算符 (Unary Operators)

语法 (Syntax)

UnaryExpression[Yield, Await] : UpdateExpression[?Yield, ?Await] delete UnaryExpression[?Yield, ?Await] void UnaryExpression[?Yield, ?Await] typeof UnaryExpression[?Yield, ?Await] + UnaryExpression[?Yield, ?Await] - UnaryExpression[?Yield, ?Await] ~ UnaryExpression[?Yield, ?Await] ! UnaryExpression[?Yield, ?Await] [+Await] AwaitExpression[?Yield]

13.5.1 delete 运算符 (The delete Operator)

13.5.1.1 静态语义:早期错误 (Static Semantics: Early Errors)

UnaryExpression : delete UnaryExpression Note

最后一条规则意味着诸如 delete (((foo))) 的表达式会因为递归应用首条规则而产生早期错误。

13.5.1.2 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : delete UnaryExpression
  1. ref 为 ? Evaluation of UnaryExpression
  2. ref 不是 Reference Record,返回 true
  3. IsUnresolvableReference(ref) 为 true,则
    1. 断言:ref.[[Strict]]false
    2. 返回 true
  4. IsPropertyReference(ref) 为 true,则
    1. 断言:IsPrivateReference(ref) 为 false
    2. IsSuperReference(ref) 为 true,抛出 ReferenceError 异常。
    3. baseObj 为 ? ToObject(ref.[[Base]])。
    4. ref.[[ReferencedName]] 不是属性键,则
      1. ref.[[ReferencedName]] 为 ? ToPropertyKey(ref.[[ReferencedName]])。
    5. deleteStatus 为 ? baseObj.[[Delete]](ref.[[ReferencedName]])。
    6. deleteStatusfalseref.[[Strict]]true,抛出 TypeError 异常。
    7. 返回 deleteStatus
  5. 否则,
    1. baseref.[[Base]]
    2. 断言:baseEnvironment Record
    3. 返回 ? base.DeleteBinding(ref.[[ReferencedName]])。
Note 1

在严格模式代码中出现的 delete 运算符,如果其 UnaryExpression 是对变量、函数参数或函数名的直接引用,则抛出 SyntaxError;另外,若删除的属性具有 { [[Configurable]]: false }(或以其他方式不可删除),则抛出 TypeError

Note 2

步骤 4.c 可能创建的对象在该抽象操作普通对象 [[Delete]] 内部方法之外不可访问,实现可选择避免实际创建该对象。

13.5.2 void 运算符 (The void Operator)

13.5.2.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : void UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. 执行 ? GetValue(expr)。
  3. 返回 undefined
Note

必须调用 GetValue(即使其结果未使用),因为它可能具有可观察的副作用。

13.5.3 typeof 运算符 (The typeof Operator)

13.5.3.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : typeof UnaryExpression
  1. val 为 ? Evaluation of UnaryExpression
  2. valReference Record,则
    1. IsUnresolvableReference(val) 为 true,返回 "undefined"
  3. val 为 ? GetValue(val)。
  4. valundefined,返回 "undefined"
  5. valnull,返回 "object"
  6. val 为 String,返回 "string"
  7. val 为 Symbol,返回 "symbol"
  8. val 为 Boolean,返回 "boolean"
  9. val 为 Number,返回 "number"
  10. val 为 BigInt,返回 "bigint"
  11. 断言:val 为 Object。
  12. Normative Optional
    宿主是 web 浏览器或否则支持 [[IsHTMLDDA]] 内部槽,则
    1. val 拥有 [[IsHTMLDDA]] 内部槽,返回 "undefined"
  13. val 拥有 [[Call]] 内部方法,返回 "function"
  14. 返回 "object"

13.5.4 一元 + 运算符 (Unary + Operator)

Note

一元 + 运算符将其操作数转换为 Number 类型

13.5.4.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : + UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. 返回 ? ToNumber(? GetValue(expr))。

13.5.5 一元 - 运算符 (Unary - Operator)

Note

一元 - 运算符将其操作数先转换为数值再取相反数。对 +0𝔽 取负得到 -0𝔽;对 -0𝔽 取负得到 +0𝔽

13.5.5.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : - UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. oldValue 为 ? ToNumeric(? GetValue(expr))。
  3. oldValue 是 Number,则
    1. 返回 Number::unaryMinus(oldValue)。
  4. 否则,
    1. 断言:oldValue 为 BigInt。
    2. 返回 BigInt::unaryMinus(oldValue)。

13.5.6 按位取反运算符 (Bitwise NOT Operator ~)

13.5.6.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : ~ UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. oldValue 为 ? ToNumeric(? GetValue(expr))。
  3. oldValue 是 Number,则
    1. 返回 Number::bitwiseNOT(oldValue)。
  4. 否则,
    1. 断言:oldValue 为 BigInt。
    2. 返回 BigInt::bitwiseNOT(oldValue)。

13.5.7 逻辑非运算符 (Logical NOT Operator !)

13.5.7.1 运行时语义:求值 (Runtime Semantics: Evaluation)

UnaryExpression : ! UnaryExpression
  1. expr 为 ? Evaluation of UnaryExpression
  2. oldValueToBoolean(? GetValue(expr))。
  3. oldValuetrue,返回 false
  4. 返回 true

13.6 幂运算符 (Exponentiation Operator)

语法 (Syntax)

ExponentiationExpression[Yield, Await] : UnaryExpression[?Yield, ?Await] UpdateExpression[?Yield, ?Await] ** ExponentiationExpression[?Yield, ?Await]

13.6.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ExponentiationExpression : UpdateExpression ** ExponentiationExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(UpdateExpression, **, ExponentiationExpression)。

13.7 乘法类运算符 (Multiplicative Operators)

语法 (Syntax)

MultiplicativeExpression[Yield, Await] : ExponentiationExpression[?Yield, ?Await] MultiplicativeExpression[?Yield, ?Await] MultiplicativeOperator ExponentiationExpression[?Yield, ?Await] MultiplicativeOperator : one of * / % Note
  • * 运算符执行乘法,产生其操作数的乘积。
  • / 运算符执行除法,产生其操作数的商。
  • % 运算符产生隐含除法后的余数。

13.7.1 运行时语义:求值 (Runtime Semantics: Evaluation)

MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
  1. opTextMultiplicativeOperator 匹配的源文本。
  2. 返回 ? EvaluateStringOrNumericBinaryExpression(MultiplicativeExpression, opText, ExponentiationExpression)。

13.8 加法类运算符 (Additive Operators)

语法 (Syntax)

AdditiveExpression[Yield, Await] : MultiplicativeExpression[?Yield, ?Await] AdditiveExpression[?Yield, ?Await] + MultiplicativeExpression[?Yield, ?Await] AdditiveExpression[?Yield, ?Await] - MultiplicativeExpression[?Yield, ?Await]

13.8.1 加法运算符 (The Addition Operator +)

Note

加法运算符执行字符串连接或数值加法。

13.8.1.1 运行时语义:求值 (Runtime Semantics: Evaluation)

AdditiveExpression : AdditiveExpression + MultiplicativeExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(AdditiveExpression, +, MultiplicativeExpression)。

13.8.2 减法运算符 (The Subtraction Operator -)

Note

- 运算符执行减法,产生操作数之差。

13.8.2.1 运行时语义:求值 (Runtime Semantics: Evaluation)

AdditiveExpression : AdditiveExpression - MultiplicativeExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(AdditiveExpression, -, MultiplicativeExpression)。

13.9 位移运算符 (Bitwise Shift Operators)

语法 (Syntax)

ShiftExpression[Yield, Await] : AdditiveExpression[?Yield, ?Await] ShiftExpression[?Yield, ?Await] << AdditiveExpression[?Yield, ?Await] ShiftExpression[?Yield, ?Await] >> AdditiveExpression[?Yield, ?Await] ShiftExpression[?Yield, ?Await] >>> AdditiveExpression[?Yield, ?Await]

13.9.1 左移运算符 (The Left Shift Operator <<)

Note

对左操作数按右操作数指定的位数执行按位左移。

13.9.1.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ShiftExpression : ShiftExpression << AdditiveExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(ShiftExpression, <<, AdditiveExpression)。

13.9.2 带符号右移运算符 (The Signed Right Shift Operator >>)

Note

对左操作数按右操作数指定的位数执行带符号(符号位填充)按位右移。

13.9.2.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ShiftExpression : ShiftExpression >> AdditiveExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(ShiftExpression, >>, AdditiveExpression)。

13.9.3 无符号右移运算符 (The Unsigned Right Shift Operator >>>)

Note

对左操作数按右操作数指定的位数执行零填充按位右移。

13.9.3.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ShiftExpression : ShiftExpression >>> AdditiveExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(ShiftExpression, >>>, AdditiveExpression)。

13.10 关系运算符 (Relational Operators)

Note 1

关系运算符求值结果总是 Boolean,表示操作数之间关系是否成立。

语法 (Syntax)

RelationalExpression[In, Yield, Await] : ShiftExpression[?Yield, ?Await] RelationalExpression[?In, ?Yield, ?Await] < ShiftExpression[?Yield, ?Await] RelationalExpression[?In, ?Yield, ?Await] > ShiftExpression[?Yield, ?Await] RelationalExpression[?In, ?Yield, ?Await] <= ShiftExpression[?Yield, ?Await] RelationalExpression[?In, ?Yield, ?Await] >= ShiftExpression[?Yield, ?Await] RelationalExpression[?In, ?Yield, ?Await] instanceof ShiftExpression[?Yield, ?Await] [+In] RelationalExpression[+In, ?Yield, ?Await] in ShiftExpression[?Yield, ?Await] [+In] PrivateIdentifier in ShiftExpression[?Yield, ?Await] Note 2

[In] 语法参数用于避免把关系表达式中的 in 运算符与 for 语句中的 in 混淆。

13.10.1 运行时语义:求值 (Runtime Semantics: Evaluation)

RelationalExpression : RelationalExpression < ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. r 为 ? IsLessThan(lVal, rVal, true)。
  6. rundefined,返回 false;否则返回 r
RelationalExpression : RelationalExpression > ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. r 为 ? IsLessThan(rVal, lVal, false)。
  6. rundefined,返回 false;否则返回 r
RelationalExpression : RelationalExpression <= ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. r 为 ? IsLessThan(rVal, lVal, false)。
  6. rtrueundefined,返回 false;否则返回 true
RelationalExpression : RelationalExpression >= ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. r 为 ? IsLessThan(lVal, rVal, true)。
  6. rtrueundefined,返回 false;否则返回 true
RelationalExpression : RelationalExpression instanceof ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. 返回 ? InstanceofOperator(lVal, rVal)。
RelationalExpression : RelationalExpression in ShiftExpression
  1. lRef 为 ? Evaluation of RelationalExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of ShiftExpression
  4. rVal 为 ? GetValue(rRef)。
  5. rVal 不是 Object,抛出 TypeError 异常。
  6. 返回 ? HasProperty(rVal, ? ToPropertyKey(lVal))。
RelationalExpression : PrivateIdentifier in ShiftExpression
  1. privateIdentifierPrivateIdentifier 的 StringValue。
  2. rRef 为 ? Evaluation of ShiftExpression
  3. rVal 为 ? GetValue(rRef)。
  4. rVal 不是 Object,抛出 TypeError 异常。
  5. privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  6. 断言:privateEnvnull
  7. privateNameResolvePrivateIdentifier(privateEnv, privateIdentifier)。
  8. PrivateElementFind(rVal, privateName) 非 empty,返回 true
  9. 返回 false

13.10.2 InstanceofOperator ( V, target )

The abstract operation InstanceofOperator takes arguments V (an ECMAScript language value) and target (an ECMAScript language value) and returns 返回一个含 Boolean 的正常完成或一个 throw completion. 实现判定 V 是否为 target 实例的通用算法:若 target 定义 %Symbol.hasInstance% 方法则调用之,否则检查 target"prototype" 属性值是否出现在 V 的原型链上。 It performs the following steps when called:

  1. target 不是 Object,抛出 TypeError 异常。
  2. instOfHandler 为 ? GetMethod(target, %Symbol.hasInstance%)。
  3. instOfHandler 不为 undefined,则
    1. 返回 ToBoolean(? Call(instOfHandler, target, « V »))。
  4. IsCallable(target) 为 false,抛出 TypeError 异常。
  5. 返回 ? OrdinaryHasInstance(target, V)。
Note

步骤 45 兼容早期未使用 %Symbol.hasInstance% 定义 instanceof 语义的版本。若对象未定义或继承该方法,则使用默认语义。

13.11 相等运算符 (Equality Operators)

Note

相等运算符求值结果总是 Boolean,表示两操作数之间命名关系是否成立。

语法 (Syntax)

EqualityExpression[In, Yield, Await] : RelationalExpression[?In, ?Yield, ?Await] EqualityExpression[?In, ?Yield, ?Await] == RelationalExpression[?In, ?Yield, ?Await] EqualityExpression[?In, ?Yield, ?Await] != RelationalExpression[?In, ?Yield, ?Await] EqualityExpression[?In, ?Yield, ?Await] === RelationalExpression[?In, ?Yield, ?Await] EqualityExpression[?In, ?Yield, ?Await] !== RelationalExpression[?In, ?Yield, ?Await]

13.11.1 运行时语义:求值 (Runtime Semantics: Evaluation)

EqualityExpression : EqualityExpression == RelationalExpression
  1. lRef 为 ? Evaluation of EqualityExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of RelationalExpression
  4. rVal 为 ? GetValue(rRef)。
  5. 返回 ? IsLooselyEqual(rVal, lVal)。
EqualityExpression : EqualityExpression != RelationalExpression
  1. lRef 为 ? Evaluation of EqualityExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of RelationalExpression
  4. rVal 为 ? GetValue(rRef)。
  5. r 为 ? IsLooselyEqual(rVal, lVal)。
  6. rtrue,返回 false;否则返回 true
EqualityExpression : EqualityExpression === RelationalExpression
  1. lRef 为 ? Evaluation of EqualityExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of RelationalExpression
  4. rVal 为 ? GetValue(rRef)。
  5. 返回 IsStrictlyEqual(rVal, lVal)。
EqualityExpression : EqualityExpression !== RelationalExpression
  1. lRef 为 ? Evaluation of EqualityExpression
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of RelationalExpression
  4. rVal 为 ? GetValue(rRef)。
  5. rIsStrictlyEqual(rVal, lVal)。
  6. rtrue,返回 false;否则返回 true
Note 1

基于上述定义:

  • 可通过 `${a}` == `${b}` 强制字符串比较。
  • 可通过 +a == +b 强制数值比较。
  • 可通过 !a == !b 强制布尔比较。
Note 2

相等运算符保持以下不变式:

  • A != B 等价于 !(A == B)
  • A == B 等价于 B == A,除求值顺序外。
Note 3

相等运算符并非总是传递。例如可能存在两个不同的 String 对象,各表示相同的字符串值;它们分别与该字符串字面值 ==true,但二者彼此 ==false。示例:

  • new String("a") == "a""a" == new String("a") 均为 true
  • new String("a") == new String("a")false
Note 4

字符串比较对代码单元序列执行简单相等测试,不使用 Unicode 规范中更复杂的语义性字符/字符串等价与排序定义。故按 Unicode 标准正则等价的字符串值可能测试为不等;此算法假设两者已规范化。

13.12 二元按位运算符 (Binary Bitwise Operators)

语法 (Syntax)

BitwiseANDExpression[In, Yield, Await] : EqualityExpression[?In, ?Yield, ?Await] BitwiseANDExpression[?In, ?Yield, ?Await] & EqualityExpression[?In, ?Yield, ?Await] BitwiseXORExpression[In, Yield, Await] : BitwiseANDExpression[?In, ?Yield, ?Await] BitwiseXORExpression[?In, ?Yield, ?Await] ^ BitwiseANDExpression[?In, ?Yield, ?Await] BitwiseORExpression[In, Yield, Await] : BitwiseXORExpression[?In, ?Yield, ?Await] BitwiseORExpression[?In, ?Yield, ?Await] | BitwiseXORExpression[?In, ?Yield, ?Await]

13.12.1 运行时语义:求值 (Runtime Semantics: Evaluation)

BitwiseANDExpression : BitwiseANDExpression & EqualityExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(BitwiseANDExpression, &, EqualityExpression)。
BitwiseXORExpression : BitwiseXORExpression ^ BitwiseANDExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(BitwiseXORExpression, ^, BitwiseANDExpression)。
BitwiseORExpression : BitwiseORExpression | BitwiseXORExpression
  1. 返回 ? EvaluateStringOrNumericBinaryExpression(BitwiseORExpression, |, BitwiseXORExpression)。

13.13 二元逻辑运算符 (Binary Logical Operators)

语法 (Syntax)

LogicalANDExpression[In, Yield, Await] : BitwiseORExpression[?In, ?Yield, ?Await] LogicalANDExpression[?In, ?Yield, ?Await] && BitwiseORExpression[?In, ?Yield, ?Await] LogicalORExpression[In, Yield, Await] : LogicalANDExpression[?In, ?Yield, ?Await] LogicalORExpression[?In, ?Yield, ?Await] || LogicalANDExpression[?In, ?Yield, ?Await] CoalesceExpression[In, Yield, Await] : CoalesceExpressionHead[?In, ?Yield, ?Await] ?? BitwiseORExpression[?In, ?Yield, ?Await] CoalesceExpressionHead[In, Yield, Await] : CoalesceExpression[?In, ?Yield, ?Await] BitwiseORExpression[?In, ?Yield, ?Await] ShortCircuitExpression[In, Yield, Await] : LogicalORExpression[?In, ?Yield, ?Await] CoalesceExpression[?In, ?Yield, ?Await] Note

&&|| 运算符产生的值不一定是 Boolean,而总是两个操作数表达式之一的值。

13.13.1 运行时语义:求值 (Runtime Semantics: Evaluation)

LogicalANDExpression : LogicalANDExpression && BitwiseORExpression
  1. lRef 为 ? Evaluation of LogicalANDExpression
  2. lVal 为 ? GetValue(lRef)。
  3. ToBoolean(lVal) 为 false,返回 lVal
  4. rRef 为 ? Evaluation of BitwiseORExpression
  5. 返回 ? GetValue(rRef)。
LogicalORExpression : LogicalORExpression || LogicalANDExpression
  1. lRef 为 ? Evaluation of LogicalORExpression
  2. lVal 为 ? GetValue(lRef)。
  3. ToBoolean(lVal) 为 true,返回 lVal
  4. rRef 为 ? Evaluation of LogicalANDExpression
  5. 返回 ? GetValue(rRef)。
CoalesceExpression : CoalesceExpressionHead ?? BitwiseORExpression
  1. lRef 为 ? Evaluation of CoalesceExpressionHead
  2. lVal 为 ? GetValue(lRef)。
  3. lValundefinednull,则
    1. rRef 为 ? Evaluation of BitwiseORExpression
    2. 返回 ? GetValue(rRef)。
  4. 否则,
    1. 返回 lVal

13.14 条件运算符 ( ? : ) (Conditional Operator ( ? : ))

语法 (Syntax)

ConditionalExpression[In, Yield, Await] : ShortCircuitExpression[?In, ?Yield, ?Await] ShortCircuitExpression[?In, ?Yield, ?Await] ? AssignmentExpression[+In, ?Yield, ?Await] : AssignmentExpression[?In, ?Yield, ?Await] Note

ECMAScript 中 ConditionalExpression 的语法与 C 和 Java 略有不同;后两者允许第二个子表达式是 Expression,但限制第三个表达式是 ConditionalExpression。ECMAScript 这样设计的动机是允许条件两支任一侧控制一个赋值表达式,并移除“逗号表达式作为中间表达式”这一令人困惑且基本无用的情形。

13.14.1 运行时语义:求值 (Runtime Semantics: Evaluation)

ConditionalExpression : ShortCircuitExpression ? AssignmentExpression : AssignmentExpression
  1. lRef 为 ? Evaluation of ShortCircuitExpression
  2. lValToBoolean(? GetValue(lRef))。
  3. lValtrue,则
    1. trueRef 为 ? Evaluation of 第一个 AssignmentExpression
    2. 返回 ? GetValue(trueRef)。
  4. 否则,
    1. falseRef 为 ? Evaluation of 第二个 AssignmentExpression
    2. 返回 ? GetValue(falseRef)。

13.15 赋值运算符 (Assignment Operators)

语法 (Syntax)

AssignmentExpression[In, Yield, Await] : ConditionalExpression[?In, ?Yield, ?Await] [+Yield] YieldExpression[?In, ?Await] ArrowFunction[?In, ?Yield, ?Await] AsyncArrowFunction[?In, ?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] = AssignmentExpression[?In, ?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] AssignmentOperator AssignmentExpression[?In, ?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] &&= AssignmentExpression[?In, ?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] ||= AssignmentExpression[?In, ?Yield, ?Await] LeftHandSideExpression[?Yield, ?Await] ??= AssignmentExpression[?In, ?Yield, ?Await] AssignmentOperator : one of *= /= %= += -= <<= >>= >>>= &= ^= |= **=

13.15.1 静态语义:早期错误 (Static Semantics: Early Errors)

AssignmentExpression : LeftHandSideExpression = AssignmentExpression AssignmentExpression : LeftHandSideExpression AssignmentOperator AssignmentExpression AssignmentExpression : LeftHandSideExpression &&= AssignmentExpression LeftHandSideExpression ||= AssignmentExpression LeftHandSideExpression ??= AssignmentExpression

13.15.2 运行时语义:求值 (Runtime Semantics: Evaluation)

AssignmentExpression : LeftHandSideExpression = AssignmentExpression
  1. LeftHandSideExpression 既不是 ObjectLiteral 也不是 ArrayLiteral,则
    1. lRef 为 ? Evaluation of LeftHandSideExpression
    2. LeftHandSideExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
    3. 若 IsAnonymousFunctionDefinition(AssignmentExpression) 为 true 且 IsIdentifierRef of LeftHandSideExpressiontrue,则
      1. lhsLeftHandSideExpression 的 StringValue。
      2. rVal 为 ? NamedEvaluation of AssignmentExpression,参数 lhs
    4. 否则,
      1. rRef 为 ? Evaluation of AssignmentExpression
      2. rVal 为 ? GetValue(rRef)。
    5. 执行 ? PutValue(lRef, rVal)。
    6. 返回 rVal
  2. assignmentPatternLeftHandSideExpression 覆盖AssignmentPattern
  3. rRef 为 ? Evaluation of AssignmentExpression
  4. rVal 为 ? GetValue(rRef)。
  5. 执行 ? DestructuringAssignmentEvaluation of assignmentPattern,参数 rVal
  6. 返回 rVal
AssignmentExpression : LeftHandSideExpression AssignmentOperator AssignmentExpression
  1. lRef 为 ? Evaluation of LeftHandSideExpression
  2. LeftHandSideExpression 的 AssignmentTargetType 为 web-compat,抛出 ReferenceError 异常。
  3. lVal 为 ? GetValue(lRef)。
  4. rRef 为 ? Evaluation of AssignmentExpression
  5. rVal 为 ? GetValue(rRef)。
  6. assignmentOpTextAssignmentOperator 所匹配的源文本。
  7. opText 为下表中与 assignmentOpText 关联的 Unicode 码点序列:
    assignmentOpText opText
    **= **
    *= *
    /= /
    %= %
    += +
    -= -
    <<= <<
    >>= >>
    >>>= >>>
    &= &
    ^= ^
    |= |
  8. r 为 ? ApplyStringOrNumericBinaryOperator(lVal, opText, rVal)。
  9. 执行 ? PutValue(lRef, r)。
  10. 返回 r
AssignmentExpression : LeftHandSideExpression &&= AssignmentExpression
  1. lRef 为 ? Evaluation of LeftHandSideExpression
  2. lVal 为 ? GetValue(lRef)。
  3. ToBoolean(lVal) 为 false,返回 lVal
  4. 若 IsAnonymousFunctionDefinition(AssignmentExpression) 为 true 且 IsIdentifierRef of LeftHandSideExpressiontrue,则
    1. lhsLeftHandSideExpression 的 StringValue。
    2. rVal 为 ? NamedEvaluation of AssignmentExpression,参数 lhs
  5. 否则,
    1. rRef 为 ? Evaluation of AssignmentExpression
    2. rVal 为 ? GetValue(rRef)。
  6. 执行 ? PutValue(lRef, rVal)。
  7. 返回 rVal
AssignmentExpression : LeftHandSideExpression ||= AssignmentExpression
  1. lRef 为 ? Evaluation of LeftHandSideExpression
  2. lVal 为 ? GetValue(lRef)。
  3. ToBoolean(lVal) 为 true,返回 lVal
  4. 若 IsAnonymousFunctionDefinition(AssignmentExpression) 为 true 且 IsIdentifierRef of LeftHandSideExpressiontrue,则
    1. lhsLeftHandSideExpression 的 StringValue。
    2. rVal 为 ? NamedEvaluation of AssignmentExpression,参数 lhs
  5. 否则,
    1. rRef 为 ? Evaluation of AssignmentExpression
    2. rVal 为 ? GetValue(rRef)。
  6. 执行 ? PutValue(lRef, rVal)。
  7. 返回 rVal
AssignmentExpression : LeftHandSideExpression ??= AssignmentExpression
  1. lRef 为 ? Evaluation of LeftHandSideExpression
  2. lVal 为 ? GetValue(lRef)。
  3. lVal 既不是 undefined 也不是 null,返回 lVal
  4. 若 IsAnonymousFunctionDefinition(AssignmentExpression) 为 true 且 IsIdentifierRef of LeftHandSideExpressiontrue,则
    1. lhsLeftHandSideExpression 的 StringValue。
    2. rVal 为 ? NamedEvaluation of AssignmentExpression,参数 lhs
  5. 否则,
    1. rRef 为 ? Evaluation of AssignmentExpression
    2. rVal 为 ? GetValue(rRef)。
  6. 执行 ? PutValue(lRef, rVal)。
  7. 返回 rVal
Note

当该表达式出现在严格模式代码中时,若步骤 1.e3222 中的 lRef 是不可解析引用,则抛出 ReferenceError。另外,若步骤 9666 中的 lRef 引用一个 { [[Writable]]: false } 的数据属性[[Set]]undefined访问器属性,或对象中不存在且该对象的 IsExtensible 结果为 false 的属性,则抛出 TypeError

13.15.3 ApplyStringOrNumericBinaryOperator ( lVal, opText, rVal )

The abstract operation ApplyStringOrNumericBinaryOperator takes arguments lVal (一个 ECMAScript 语言值), opText (**, *, /, %, +, -, <<, >>, >>>, &, ^, 或 |), and rVal (一个 ECMAScript 语言值) and returns 返回一个含 String、BigInt 或 Number 的正常完成,或一个 throw completion. It performs the following steps when called:

  1. opText+,则
    1. lPrim 为 ? ToPrimitive(lVal)。
    2. rPrim 为 ? ToPrimitive(rVal)。
    3. lPrim 为 String 或 rPrim 为 String,则
      1. lStr 为 ? ToString(lPrim)。
      2. rStr 为 ? ToString(rPrim)。
      3. 返回 lStrrStr 的字符串连接。
    4. lVallPrim
    5. rValrPrim
  2. 注:此时必为数值运算。
  3. lNum 为 ? ToNumeric(lVal)。
  4. rNum 为 ? ToNumeric(rVal)。
  5. SameType(lNum, rNum) 为 false,抛出 TypeError 异常。
  6. lNum 为 BigInt,则
    1. opText**,返回 ? BigInt::exponentiate(lNum, rNum)。
    2. opText/,返回 ? BigInt::divide(lNum, rNum)。
    3. opText%,返回 ? BigInt::remainder(lNum, rNum)。
    4. opText>>>,返回 ? BigInt::unsignedRightShift(lNum, rNum)。
    5. operation 为下表中与 opText 关联的抽象操作
      opText operation
      * BigInt::multiply
      + BigInt::add
      - BigInt::subtract
      << BigInt::leftShift
      >> BigInt::signedRightShift
      & BigInt::bitwiseAND
      ^ BigInt::bitwiseXOR
      | BigInt::bitwiseOR
  7. 否则,
    1. 断言:lNum 为 Number。
    2. operation 为下表中与 opText 关联的抽象操作
      opText operation
      ** Number::exponentiate
      * Number::multiply
      / Number::divide
      % Number::remainder
      + Number::add
      - Number::subtract
      << Number::leftShift
      >> Number::signedRightShift
      >>> Number::unsignedRightShift
      & Number::bitwiseAND
      ^ Number::bitwiseXOR
      | Number::bitwiseOR
  8. 返回 operation(lNum, rNum)。
Note 1

步骤 1.a1.b 中对 ToPrimitive 的调用不提供 hint。除 Date 外的所有标准对象在缺省 hint 时按 number 处理;Date 按 string 处理;特异对象可按其他方式处理。

Note 2

步骤 1.cIsLessThan 算法的步骤 3 不同:它使用逻辑“或”而不是逻辑“与”。

13.15.4 EvaluateStringOrNumericBinaryExpression ( leftOperand, opText, rightOperand )

The abstract operation EvaluateStringOrNumericBinaryExpression takes arguments leftOperand (一个 Parse Node), opText (一个 Unicode 码点序列), and rightOperand (一个 Parse Node) and returns 返回一个含 String、BigInt 或 Number 的正常完成,或一个突然完成. It performs the following steps when called:

  1. lRef 为 ? Evaluation of leftOperand
  2. lVal 为 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of rightOperand
  4. rVal 为 ? GetValue(rRef)。
  5. 返回 ? ApplyStringOrNumericBinaryOperator(lVal, opText, rVal)。

13.15.5 解构赋值 (Destructuring Assignment)

补充语法 (Supplemental Syntax)

在处理产生式实例:
AssignmentExpression : LeftHandSideExpression = AssignmentExpression
时,对 LeftHandSideExpression 的解释通过下列语法细化:

AssignmentPattern[Yield, Await] : ObjectAssignmentPattern[?Yield, ?Await] ArrayAssignmentPattern[?Yield, ?Await] ObjectAssignmentPattern[Yield, Await] : { } { AssignmentRestProperty[?Yield, ?Await] } { AssignmentPropertyList[?Yield, ?Await] } { AssignmentPropertyList[?Yield, ?Await] , AssignmentRestProperty[?Yield, ?Await]opt } ArrayAssignmentPattern[Yield, Await] : [ Elisionopt AssignmentRestElement[?Yield, ?Await]opt ] [ AssignmentElementList[?Yield, ?Await] ] [ AssignmentElementList[?Yield, ?Await] , Elisionopt AssignmentRestElement[?Yield, ?Await]opt ] AssignmentRestProperty[Yield, Await] : ... DestructuringAssignmentTarget[?Yield, ?Await] AssignmentPropertyList[Yield, Await] : AssignmentProperty[?Yield, ?Await] AssignmentPropertyList[?Yield, ?Await] , AssignmentProperty[?Yield, ?Await] AssignmentElementList[Yield, Await] : AssignmentElisionElement[?Yield, ?Await] AssignmentElementList[?Yield, ?Await] , AssignmentElisionElement[?Yield, ?Await] AssignmentElisionElement[Yield, Await] : Elisionopt AssignmentElement[?Yield, ?Await] AssignmentProperty[Yield, Await] : IdentifierReference[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt PropertyName[?Yield, ?Await] : AssignmentElement[?Yield, ?Await] AssignmentElement[Yield, Await] : DestructuringAssignmentTarget[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt AssignmentRestElement[Yield, Await] : ... DestructuringAssignmentTarget[?Yield, ?Await] DestructuringAssignmentTarget[Yield, Await] : LeftHandSideExpression[?Yield, ?Await]

13.15.5.1 静态语义:早期错误 (Static Semantics: Early Errors)

AssignmentProperty : IdentifierReference Initializeropt AssignmentRestProperty : ... DestructuringAssignmentTarget DestructuringAssignmentTarget : LeftHandSideExpression

13.15.5.2 运行时语义:DestructuringAssignmentEvaluation

The syntax-directed operation 运行时语义:DestructuringAssignmentEvaluation takes argument value (一个 ECMAScript 语言值) and returns 返回一个含 unused正常完成或一个突然完成. It is defined piecewise over the following productions:

ObjectAssignmentPattern : { }
  1. 执行 ? RequireObjectCoercible(value)。
  2. 返回 unused
ObjectAssignmentPattern : { AssignmentPropertyList } { AssignmentPropertyList , }
  1. 执行 ? RequireObjectCoercible(value)。
  2. 执行 ? PropertyDestructuringAssignmentEvaluation of AssignmentPropertyList,参数 value
  3. 返回 unused
ObjectAssignmentPattern : { AssignmentRestProperty }
  1. 执行 ? RequireObjectCoercible(value)。
  2. excludedNames 为新空列表。
  3. 返回 ? RestDestructuringAssignmentEvaluation of AssignmentRestProperty,参数 valueexcludedNames
ObjectAssignmentPattern : { AssignmentPropertyList , AssignmentRestProperty }
  1. 执行 ? RequireObjectCoercible(value)。
  2. excludedNames 为 ? PropertyDestructuringAssignmentEvaluation of AssignmentPropertyList,参数 value
  3. 返回 ? RestDestructuringAssignmentEvaluation of AssignmentRestProperty,参数 valueexcludedNames
ArrayAssignmentPattern : [ ]
  1. iteratorRecord 为 ? GetIterator(value, sync)。
  2. 返回 ? IteratorClose(iteratorRecord, NormalCompletion(unused))。
ArrayAssignmentPattern : [ Elision ]
  1. iteratorRecord 为 ? GetIterator(value, sync)。
  2. resultCompletion(IteratorDestructuringAssignmentEvaluation of Elision,参数 iteratorRecord)。
  3. iteratorRecord.[[Done]]false,返回 ? IteratorClose(iteratorRecord, result)。
  4. 返回 result
ArrayAssignmentPattern : [ Elisionopt AssignmentRestElement ]
  1. iteratorRecord 为 ? GetIterator(value, sync)。
  2. 若存在 Elision,则
    1. statusCompletion(IteratorDestructuringAssignmentEvaluation of Elision,参数 iteratorRecord)。
    2. status 是突然完成,则
      1. 断言:iteratorRecord.[[Done]]true
      2. 返回 ? status
  3. resultCompletion(IteratorDestructuringAssignmentEvaluation of AssignmentRestElement,参数 iteratorRecord)。
  4. iteratorRecord.[[Done]]false,返回 ? IteratorClose(iteratorRecord, result)。
  5. 返回 result
ArrayAssignmentPattern : [ AssignmentElementList ]
  1. iteratorRecord 为 ? GetIterator(value, sync)。
  2. resultCompletion(IteratorDestructuringAssignmentEvaluation of AssignmentElementList,参数 iteratorRecord)。
  3. iteratorRecord.[[Done]]false,返回 ? IteratorClose(iteratorRecord, result)。
  4. 返回 result
ArrayAssignmentPattern : [ AssignmentElementList , Elisionopt AssignmentRestElementopt ]
  1. iteratorRecord 为 ? GetIterator(value, sync)。
  2. statusCompletion(IteratorDestructuringAssignmentEvaluation of AssignmentElementList,参数 iteratorRecord)。
  3. status 是突然完成,则
    1. iteratorRecord.[[Done]]false,返回 ? IteratorClose(iteratorRecord, status)。
    2. 返回 ? status
  4. 若存在 Elision,则
    1. statusCompletion(IteratorDestructuringAssignmentEvaluation of Elision,参数 iteratorRecord)。
    2. status 是突然完成,则
      1. 断言:iteratorRecord.[[Done]]true
      2. 返回 ? status
  5. 若存在 AssignmentRestElement,则
    1. statusCompletion(IteratorDestructuringAssignmentEvaluation of AssignmentRestElement,参数 iteratorRecord)。
  6. iteratorRecord.[[Done]]false,返回 ? IteratorClose(iteratorRecord, status)。
  7. 返回 ? status

13.15.5.3 运行时语义:PropertyDestructuringAssignmentEvaluation

The syntax-directed operation 运行时语义:PropertyDestructuringAssignmentEvaluation takes argument value (一个 ECMAScript 语言值) and returns 返回一个含属性键列表的正常完成或一个突然完成. 收集所有被解构的属性键列表。 It is defined piecewise over the following productions:

AssignmentPropertyList : AssignmentPropertyList , AssignmentProperty
  1. propertyNames 为 ? PropertyDestructuringAssignmentEvaluation of AssignmentPropertyList,参数 value
  2. nextNames 为 ? PropertyDestructuringAssignmentEvaluation of AssignmentProperty,参数 value
  3. 返回 propertyNamesnextNames 的列表连接。
AssignmentProperty : IdentifierReference Initializeropt
  1. PIdentifierReference 的 StringValue。
  2. lRef 为 ? ResolveBinding(P)。
  3. v 为 ? GetV(value, P)。
  4. 若存在 Initializervundefined,则
    1. 若 IsAnonymousFunctionDefinition(Initializer) 为 true,则
      1. v 为 ? NamedEvaluation of Initializer,参数 P
    2. 否则,
      1. defaultValue 为 ? Evaluation of Initializer
      2. v 为 ? GetValue(defaultValue)。
  5. 执行 ? PutValue(lRef, v)。
  6. 返回 « P »。
AssignmentProperty : PropertyName : AssignmentElement
  1. name 为 ? Evaluation of PropertyName
  2. 执行 ? KeyedDestructuringAssignmentEvaluation of AssignmentElement,参数 valuename
  3. 返回 « name »。

13.15.5.4 运行时语义:RestDestructuringAssignmentEvaluation

The syntax-directed operation 运行时语义:RestDestructuringAssignmentEvaluation takes arguments value (一个 ECMAScript 语言值) and excludedNames (一个属性键列表) and returns 返回一个含 unused正常完成或一个突然完成. It is defined piecewise over the following productions:

AssignmentRestProperty : ... DestructuringAssignmentTarget
  1. lRef 为 ? Evaluation of DestructuringAssignmentTarget
  2. restObjOrdinaryObjectCreate(%Object.prototype%)。
  3. 执行 ? CopyDataProperties(restObj, value, excludedNames)。
  4. 返回 ? PutValue(lRef, restObj)。

13.15.5.5 运行时语义:IteratorDestructuringAssignmentEvaluation

The syntax-directed operation 运行时语义:IteratorDestructuringAssignmentEvaluation takes argument iteratorRecord (一个 Iterator Record) and returns 返回一个含 unused正常完成或一个突然完成. It is defined piecewise over the following productions:

AssignmentElementList : AssignmentElisionElement
  1. 返回 ? IteratorDestructuringAssignmentEvaluation of AssignmentElisionElement,参数 iteratorRecord
AssignmentElementList : AssignmentElementList , AssignmentElisionElement
  1. 执行 ? IteratorDestructuringAssignmentEvaluation of AssignmentElementList,参数 iteratorRecord
  2. 返回 ? IteratorDestructuringAssignmentEvaluation of AssignmentElisionElement,参数 iteratorRecord
AssignmentElisionElement : AssignmentElement
  1. 返回 ? IteratorDestructuringAssignmentEvaluation of AssignmentElement,参数 iteratorRecord
AssignmentElisionElement : Elision AssignmentElement
  1. 执行 ? IteratorDestructuringAssignmentEvaluation of Elision,参数 iteratorRecord
  2. 返回 ? IteratorDestructuringAssignmentEvaluation of AssignmentElement,参数 iteratorRecord
Elision : ,
  1. iteratorRecord.[[Done]]false,则
    1. 执行 ? IteratorStep(iteratorRecord)。
  2. 返回 unused
Elision : Elision ,
  1. 执行 ? IteratorDestructuringAssignmentEvaluation of Elision,参数 iteratorRecord
  2. iteratorRecord.[[Done]]false,则
    1. 执行 ? IteratorStep(iteratorRecord)。
  3. 返回 unused
AssignmentElement : DestructuringAssignmentTarget Initializeropt
  1. DestructuringAssignmentTarget 既不是 ObjectLiteral 也不是 ArrayLiteral,则
    1. lRef 为 ? Evaluation of DestructuringAssignmentTarget
  2. valueundefined
  3. iteratorRecord.[[Done]]false,则
    1. next 为 ? IteratorStepValue(iteratorRecord)。
    2. nextdone,则
      1. valuenext
  4. 若存在 Initializervalueundefined,则
    1. 若 IsAnonymousFunctionDefinition(Initializer) 为 true 且 IsIdentifierRef of DestructuringAssignmentTargettrue,则
      1. targetDestructuringAssignmentTarget 的 StringValue。
      2. v 为 ? NamedEvaluation of Initializer,参数 target
    2. 否则,
      1. defaultValue 为 ? Evaluation of Initializer
      2. v 为 ? GetValue(defaultValue)。
  5. 否则,
    1. vvalue
  6. DestructuringAssignmentTargetObjectLiteralArrayLiteral,则
    1. nestedAssignmentPatternDestructuringAssignmentTarget 覆盖AssignmentPattern
    2. 返回 ? DestructuringAssignmentEvaluation of nestedAssignmentPattern,参数 v
  7. 返回 ? PutValue(lRef, v)。
Note

通过在访问迭代器或求 Initializer 之前先求值非解构模式的 DestructuringAssignmentTarget,保持自左向右的求值顺序。

AssignmentRestElement : ... DestructuringAssignmentTarget
  1. DestructuringAssignmentTarget 既不是 ObjectLiteral 也不是 ArrayLiteral,则
    1. lRef 为 ? Evaluation of DestructuringAssignmentTarget
  2. A 为 ! ArrayCreate(0)。
  3. n 为 0。
  4. iteratorRecord.[[Done]]false 重复,
    1. next 为 ? IteratorStepValue(iteratorRecord)。
    2. nextdone,则
      1. 执行 ! CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), next)。
      2. nn + 1。
  5. DestructuringAssignmentTarget 既不是 ObjectLiteral 也不是 ArrayLiteral,则
    1. 返回 ? PutValue(lRef, A)。
  6. nestedAssignmentPatternDestructuringAssignmentTarget 覆盖AssignmentPattern
  7. 返回 ? DestructuringAssignmentEvaluation of nestedAssignmentPattern,参数 A

13.15.5.6 运行时语义:KeyedDestructuringAssignmentEvaluation

The syntax-directed operation 运行时语义:KeyedDestructuringAssignmentEvaluation takes arguments value (一个 ECMAScript 语言值) and propertyName (一个属性键) and returns 返回一个含 unused正常完成或一个突然完成. It is defined piecewise over the following productions:

AssignmentElement : DestructuringAssignmentTarget Initializeropt
  1. DestructuringAssignmentTarget 既不是 ObjectLiteral 也不是 ArrayLiteral,则
    1. lRef 为 ? Evaluation of DestructuringAssignmentTarget
  2. v 为 ? GetV(value, propertyName)。
  3. 若存在 Initializervundefined,则
    1. 若 IsAnonymousFunctionDefinition(Initializer) 为 true 且 IsIdentifierRef of DestructuringAssignmentTargettrue,则
      1. targetDestructuringAssignmentTarget 的 StringValue。
      2. rhsValue 为 ? NamedEvaluation of Initializer,参数 target
    2. 否则,
      1. defaultValue 为 ? Evaluation of Initializer
      2. rhsValue 为 ? GetValue(defaultValue)。
  4. 否则,
    1. rhsValuev
  5. DestructuringAssignmentTargetObjectLiteralArrayLiteral,则
    1. assignmentPatternDestructuringAssignmentTarget 覆盖AssignmentPattern
    2. 返回 ? DestructuringAssignmentEvaluation of assignmentPattern,参数 rhsValue
  6. 返回 ? PutValue(lRef, rhsValue)。

13.16 逗号运算符 ( , )

语法 (Syntax)

Expression[In, Yield, Await] : AssignmentExpression[?In, ?Yield, ?Await] Expression[?In, ?Yield, ?Await] , AssignmentExpression[?In, ?Yield, ?Await]

13.16.1 运行时语义:求值 (Runtime Semantics: Evaluation)

Expression : Expression , AssignmentExpression
  1. lRef 为 ? Evaluation of Expression
  2. 执行 ? GetValue(lRef)。
  3. rRef 为 ? Evaluation of AssignmentExpression
  4. 返回 ? GetValue(rRef)。
Note

必须调用 GetValue(即使其结果未被使用),因为它可能具有可观察的副作用。