页面模式偏好

15 ECMAScript 语言:函数和类

注

各种 ECMAScript 语言元素会创建 ECMAScript 函数对象(10.2)。对此类函数的求值始于执行其 [[Call]] 内部方法(10.2.1)。

15.1 参数列表

语法

UniqueFormalParameters[Yield, Await] : FormalParameters[?Yield, ?Await] FormalParameters[Yield, Await] : [empty] FunctionRestParameter[?Yield, ?Await] FormalParameterList[?Yield, ?Await] FormalParameterList[?Yield, ?Await] , FormalParameterList[?Yield, ?Await] , FunctionRestParameter[?Yield, ?Await] FormalParameterList[Yield, Await] : FormalParameter[?Yield, ?Await] FormalParameterList[?Yield, ?Await] , FormalParameter[?Yield, ?Await] FunctionRestParameter[Yield, Await] : BindingRestElement[?Yield, ?Await] FormalParameter[Yield, Await] : BindingElement[?Yield, ?Await]

15.1.1 Static Semantics: 提前错误

UniqueFormalParameters : FormalParameters FormalParameters : FormalParameterList 注

仅当函数具有简单参数列表且未在严格模式代码中定义时,才允许在 FormalParameterList 中多次出现相同的 BindingIdentifier。

15.1.2 Static Semantics: ContainsExpression

语法制导操作 ContainsExpression,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

ObjectBindingPattern : { } { BindingRestProperty }
  1. 返回 false。
ObjectBindingPattern : { BindingPropertyList , BindingRestProperty }
  1. 返回 BindingPropertyList 的 ContainsExpression。
ArrayBindingPattern : [ Elisionopt ]
  1. 返回 false。
ArrayBindingPattern : [ Elisionopt BindingRestElement ]
  1. 返回 BindingRestElement 的 ContainsExpression。
ArrayBindingPattern : [ BindingElementList , Elisionopt ]
  1. 返回 BindingElementList 的 ContainsExpression。
ArrayBindingPattern : [ BindingElementList , Elisionopt BindingRestElement ]
  1. 令 has 为 BindingElementList 的 ContainsExpression。
  2. 如果 has 是 true,则返回 true。
  3. 返回 BindingRestElement 的 ContainsExpression。
BindingPropertyList : BindingPropertyList , BindingProperty
  1. 令 has 为 BindingPropertyList 的 ContainsExpression。
  2. 如果 has 是 true,则返回 true。
  3. 返回 BindingProperty 的 ContainsExpression。
BindingElementList : BindingElementList , BindingElisionElement
  1. 令 has 为 BindingElementList 的 ContainsExpression。
  2. 如果 has 是 true,则返回 true。
  3. 返回 BindingElisionElement 的 ContainsExpression。
BindingElisionElement : Elisionopt BindingElement
  1. 返回 BindingElement 的 ContainsExpression。
BindingProperty : PropertyName : BindingElement
  1. 令 has 为 PropertyName 的 IsComputedPropertyKey。
  2. 如果 has 是 true,则返回 true。
  3. 返回 BindingElement 的 ContainsExpression。
BindingElement : BindingPattern Initializer
  1. 返回 true。
SingleNameBinding : BindingIdentifier
  1. 返回 false。
SingleNameBinding : BindingIdentifier Initializer
  1. 返回 true。
BindingRestElement : ... BindingIdentifier
  1. 返回 false。
BindingRestElement : ... BindingPattern
  1. 返回 BindingPattern 的 ContainsExpression。
FormalParameters : [empty]
  1. 返回 false。
FormalParameters : FormalParameterList , FunctionRestParameter
  1. 如果 FormalParameterList 的 ContainsExpression 是 true,则返回 true。
  2. 返回 FunctionRestParameter 的 ContainsExpression。
FormalParameterList : FormalParameterList , FormalParameter
  1. 如果 FormalParameterList 的 ContainsExpression 是 true,则返回 true。
  2. 返回 FormalParameter 的 ContainsExpression。
ArrowParameters : BindingIdentifier
  1. 返回 false。
ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 所覆盖的 ArrowFormalParameters。
  2. 返回 formals 的 ContainsExpression。
AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 false。

15.1.3 Static Semantics: IsSimpleParameterList

语法制导操作 IsSimpleParameterList,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

BindingElement : BindingPattern
  1. 返回 false。
BindingElement : BindingPattern Initializer
  1. 返回 false。
SingleNameBinding : BindingIdentifier
  1. 返回 true。
SingleNameBinding : BindingIdentifier Initializer
  1. 返回 false。
FormalParameters : [empty]
  1. 返回 true。
FormalParameters : FunctionRestParameter
  1. 返回 false。
FormalParameters : FormalParameterList , FunctionRestParameter
  1. 返回 false。
FormalParameterList : FormalParameterList , FormalParameter
  1. 如果 FormalParameterList 的 IsSimpleParameterList 是 false,则返回 false。
  2. 返回 FormalParameter 的 IsSimpleParameterList。
FormalParameter : BindingElement
  1. 返回 BindingElement 的 IsSimpleParameterList。
ArrowParameters : BindingIdentifier
  1. 返回 true。
ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 所覆盖的 ArrowFormalParameters。
  2. 返回 formals 的 IsSimpleParameterList。
AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 true。
CoverCallExpressionAndAsyncArrowHead : MemberExpression Arguments
  1. 令 head 为 CoverCallExpressionAndAsyncArrowHead 所覆盖的 AsyncArrowHead。
  2. 返回 head 的 IsSimpleParameterList。

15.1.4 Static Semantics: HasInitializer

语法制导操作 HasInitializer,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

BindingElement : BindingPattern
  1. 返回 false。
BindingElement : BindingPattern Initializer
  1. 返回 true。
SingleNameBinding : BindingIdentifier
  1. 返回 false。
SingleNameBinding : BindingIdentifier Initializer
  1. 返回 true。
FormalParameterList : FormalParameterList , FormalParameter
  1. 如果 FormalParameterList 的 HasInitializer 是 true,则返回 true。
  2. 返回 FormalParameter 的 HasInitializer。

15.1.5 Static Semantics: ExpectedArgumentCount

语法制导操作 ExpectedArgumentCount,接收无参数;返回值:非负整数。 它按以下产生式分别定义:

FormalParameters : [empty] FunctionRestParameter
  1. 返回 0。
FormalParameters : FormalParameterList , FunctionRestParameter
  1. 返回 FormalParameterList 的 ExpectedArgumentCount。
注

FormalParameterList 的 ExpectedArgumentCount 是剩余参数或第一个具有 Initializer 的 FormalParameter 左侧的 FormalParameters 数量。在第一个具有初始化器的参数之后允许出现没有初始化器的 FormalParameter,但这些参数被视为可选参数,其默认值为 undefined。

FormalParameterList : FormalParameter
  1. 如果 FormalParameter 的 HasInitializer 是 true,则返回 0。
  2. 返回 1。
FormalParameterList : FormalParameterList , FormalParameter
  1. 令 count 为 FormalParameterList 的 ExpectedArgumentCount。
  2. 如果 FormalParameterList 的 HasInitializer 是 true 或 FormalParameter 的 HasInitializer 是 true,则返回 count。
  3. 返回 count + 1。
ArrowParameters : BindingIdentifier
  1. 返回 1。
ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 所覆盖的 ArrowFormalParameters。
  2. 返回 formals 的 ExpectedArgumentCount。
PropertySetParameterList : FormalParameter
  1. 如果 FormalParameter 的 HasInitializer 是 true,则返回 0。
  2. 返回 1。
AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 1。

15.2 函数定义

语法

FunctionDeclaration[Yield, Await, Default] : function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } [+Default] function ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } FunctionExpression : function BindingIdentifier[~Yield, ~Await]opt ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } FunctionBody[Yield, Await] : FunctionStatementList[?Yield, ?Await] FunctionStatementList[Yield, Await] : StatementList[?Yield, ?Await, +Return]opt

15.2.1 Static Semantics: 提前错误

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody } function ( FormalParameters ) { FunctionBody } FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody } 注

FunctionBody 的 LexicallyDeclaredNames 不包括使用 var 或函数声明绑定的标识符。

FunctionBody : FunctionStatementList

15.2.2 Static Semantics: FunctionBodyContainsUseStrict

语法制导操作 FunctionBodyContainsUseStrict,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

FunctionBody : FunctionStatementList
  1. 如果 FunctionBody 的指令序言包含 Use Strict 指令,则返回 true。
  2. 返回 false。

15.2.3 Runtime Semantics: EvaluateFunctionBody

语法制导操作 EvaluateFunctionBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(返回完成 或 抛出完成)。 它按以下产生式分别定义:

FunctionBody : FunctionStatementList
  1. 执行 ? FunctionDeclarationInstantiation(funcObj, argList)。
  2. 执行 ? FunctionStatementList 的 Evaluation。
  3. 注:如果上一步产生正常完成,则求值通过越过 FunctionStatementList 的末尾而结束。
  4. 返回 ReturnCompletion(undefined)。

15.2.4 Runtime Semantics: InstantiateOrdinaryFunctionObject

语法制导操作 InstantiateOrdinaryFunctionObject,接收参数 envRecord (Environment 记录) 和 privateEnv ((PrivateEnvironment 记录 或 null));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 令 name 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 FunctionDeclaration 匹配的源文本。
  3. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  4. 执行 SetFunctionName(closure, name)。
  5. 执行 MakeConstructor(closure)。
  6. 返回 closure。
FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 令 sourceText 为 FunctionDeclaration 匹配的源文本。
  2. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  3. 执行 SetFunctionName(closure, "default")。
  4. 执行 MakeConstructor(closure)。
  5. 返回 closure。
注

匿名 FunctionDeclaration 只能作为 export default 声明的一部分出现,因此其函数代码始终是严格模式代码。

15.2.5 Runtime Semantics: InstantiateOrdinaryFunctionExpression

语法制导操作 InstantiateOrdinaryFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

FunctionExpression : function ( FormalParameters ) { FunctionBody }
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 FunctionExpression 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 SetFunctionName(closure, name)。
  7. 执行 MakeConstructor(closure)。
  8. 返回 closure。
FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 断言:不存在 name。
  2. 将 name 设置为 BindingIdentifier 的 StringValue。
  3. 令 outerEnv 为正在运行的执行上下文的 LexicalEnvironment。
  4. 令 funcEnv 为 NewDeclarativeEnvironment(outerEnv)。
  5. 执行 ! funcEnv.CreateImmutableBinding(name, false)。
  6. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  7. 令 sourceText 为 FunctionExpression 匹配的源文本。
  8. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, funcEnv, privateEnv)。
  9. 执行 SetFunctionName(closure, name)。
  10. 执行 MakeConstructor(closure)。
  11. 执行 ! funcEnv.InitializeBinding(name, closure)。
  12. 返回 closure。
注

FunctionExpression 中的 BindingIdentifier 可以从 FunctionExpression 的 FunctionBody 内部引用,从而允许函数递归调用自身。但是,与 FunctionDeclaration 不同,FunctionExpression 中的 BindingIdentifier 无法从包围该 FunctionExpression 的作用域中引用,也不会影响该作用域。

15.2.6 Runtime Semantics: Evaluation

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 empty。
注 1 FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 返回 empty。
FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody }
  1. 返回 FunctionExpression 的 InstantiateOrdinaryFunctionExpression。
注 2

对于使用 FunctionDeclaration 或 FunctionExpression 定义的每个函数,都会自动创建一个 "prototype" 属性,以允许该函数可能被用作构造函数。

FunctionStatementList : [empty]
  1. 返回 undefined。
FunctionStatementList : StatementList
  1. 令 result 为 Completion(StatementList 的 Evaluation)。
  2. 令 env 为正在运行的执行上下文的 LexicalEnvironment。
  3. 断言:env 是 Declarative Environment Record。
  4. 返回 ? DisposeResources(env.[[DisposableResourceStack]], result)。

15.3 箭头函数定义

语法

ArrowFunction[In, Yield, Await] : ArrowParameters[?Yield, ?Await] [no LineTerminator here] => ConciseBody[?In] ArrowParameters[Yield, Await] : BindingIdentifier[?Yield, ?Await] CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await] ConciseBody[In] : [lookahead ≠ {] ExpressionBody[?In, ~Await] { FunctionBody[~Yield, ~Await] } ExpressionBody[In, Await] : AssignmentExpression[?In, ~Yield, ?Await]

补充语法

当处理产生式
ArrowParameters[Yield, Await] : CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await]
的实例时,使用以下文法进一步细化对 CoverParenthesizedExpressionAndArrowParameterList 的解释:

ArrowFormalParameters[Yield, Await] : ( UniqueFormalParameters[?Yield, ?Await] )

15.3.1 Static Semantics: 提前错误

ArrowFunction : ArrowParameters => ConciseBody ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList

15.3.2 Static Semantics: ConciseBodyContainsUseStrict

语法制导操作 ConciseBodyContainsUseStrict,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

ConciseBody : ExpressionBody
  1. 返回 false。
ConciseBody : { FunctionBody }
  1. 返回 FunctionBody 的 FunctionBodyContainsUseStrict。

15.3.3 Runtime Semantics: EvaluateConciseBody

语法制导操作 EvaluateConciseBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(返回完成 或 抛出完成)。 它按以下产生式分别定义:

ConciseBody : ExpressionBody
  1. 执行 ? FunctionDeclarationInstantiation(funcObj, argList)。
  2. 返回 ? ExpressionBody 的 Evaluation。

15.3.4 Runtime Semantics: InstantiateArrowFunctionExpression

语法制导操作 InstantiateArrowFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

ArrowFunction : ArrowParameters => ConciseBody
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 ArrowFunction 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, ArrowParameters, ConciseBody, lexical-this, envRecord, privateEnv)。
  6. 执行 SetFunctionName(closure, name)。
  7. 返回 closure。
注

ArrowFunction 不会为 arguments、super、this 或 new.target 定义局部绑定。ArrowFunction 内对 arguments、super、this 或 new.target 的任何引用都必须解析到词法包围环境中的绑定。通常,这会是紧邻包围函数的 Function Environment。即使 ArrowFunction 可以包含对 super 的引用,步骤 5 中创建的函数对象也不会通过执行 MakeMethod 而成为方法。引用 super 的 ArrowFunction 始终包含在某个非 ArrowFunction 中,实现 super 所需的状态可通过由 ArrowFunction 函数对象捕获的 envRecord 访问。

15.3.5 Runtime Semantics: Evaluation

ArrowFunction : ArrowParameters => ConciseBody
  1. 返回 ArrowFunction 的 InstantiateArrowFunctionExpression。
ExpressionBody : AssignmentExpression
  1. 令 exprRef 为 ? AssignmentExpression 的 Evaluation。
  2. 令 exprValue 为 ? GetValue(exprRef)。
  3. 返回 ReturnCompletion(exprValue)。

15.4 方法定义

语法

MethodDefinition[Yield, Await] : ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } GeneratorMethod[?Yield, ?Await] AsyncMethod[?Yield, ?Await] AsyncGeneratorMethod[?Yield, ?Await] get ClassElementName[?Yield, ?Await] ( ) { FunctionBody[~Yield, ~Await] } set ClassElementName[?Yield, ?Await] ( PropertySetParameterList ) { FunctionBody[~Yield, ~Await] } PropertySetParameterList : FormalParameter[~Yield, ~Await]

15.4.1 Static Semantics: 提前错误

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }

15.4.2 Static Semantics: HasDirectSuper

语法制导操作 HasDirectSuper,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. 如果 UniqueFormalParameters Contains SuperCall 是 true,则返回 true。
  2. 返回 FunctionBody Contains SuperCall。
MethodDefinition : get ClassElementName ( ) { FunctionBody }
  1. 返回 FunctionBody Contains SuperCall。
MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }
  1. 如果 PropertySetParameterList Contains SuperCall 是 true,则返回 true。
  2. 返回 FunctionBody Contains SuperCall。
GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody }
  1. 如果 UniqueFormalParameters Contains SuperCall 是 true,则返回 true。
  2. 返回 GeneratorBody Contains SuperCall。
AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 如果 UniqueFormalParameters Contains SuperCall 是 true,则返回 true。
  2. 返回 AsyncGeneratorBody Contains SuperCall。
AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 如果 UniqueFormalParameters Contains SuperCall 是 true,则返回 true。
  2. 返回 AsyncFunctionBody Contains SuperCall。

15.4.3 Static Semantics: SpecialMethod

语法制导操作 SpecialMethod,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. 返回 false。
MethodDefinition : GeneratorMethod AsyncMethod AsyncGeneratorMethod get ClassElementName ( ) { FunctionBody } set ClassElementName ( PropertySetParameterList ) { FunctionBody }
  1. 返回 true。

15.4.4 Runtime Semantics: DefineMethod

语法制导操作 DefineMethod,接收参数 obj (Object)和可选参数 proto (Object);返回值:(正常完成 (值:记录 (字段:[[Key]] (属性键), [[Closure]] (ECMAScript 函数对象))) 或 突兀完成)。 它按以下产生式分别定义:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 如果不存在 proto,则将 proto 设置为 %Function.prototype%。
  5. 令 sourceText 为 MethodDefinition 匹配的源文本。
  6. 令 closure 为 OrdinaryFunctionCreate(proto, sourceText, UniqueFormalParameters, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  7. 执行 MakeMethod(closure, obj)。
  8. 返回 Record { [[Key]]: propertyKey, [[Closure]]: closure }。

15.4.5 Runtime Semantics: MethodDefinitionEvaluation

语法制导操作 MethodDefinitionEvaluation,接收参数 obj (Object) 和 enumerable (Boolean);返回值:(正常完成 (值:(PrivateElement 或 empty)) 或 突兀完成)。 它按以下产生式分别定义:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. 令 methodDef 为 ? 以 obj 为参数对 MethodDefinition 执行 DefineMethod 的结果。
  2. 执行 SetFunctionName(methodDef.[[Closure]], methodDef.[[Key]])。
  3. 返回 ? DefineMethodProperty(obj, methodDef.[[Key]], methodDef.[[Closure]], enumerable)。
MethodDefinition : get ClassElementName ( ) { FunctionBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 MethodDefinition 匹配的源文本。
  5. 令 formalParamList 为产生式 FormalParameters : [empty] 的一个实例。
  6. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParamList, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  7. 执行 MakeMethod(closure, obj)。
  8. 执行 SetFunctionName(closure, propertyKey, "get")。
  9. 如果 propertyKey 是 Private Name,则
    1. 返回 PrivateElement { [[Key]]: propertyKey, [[Kind]]: accessor, [[Getter]]: closure, [[Setter]]: undefined }。
  10. 令 propertyDesc 为 PropertyDescriptor { [[Getter]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }。
  11. 执行 ? DefinePropertyOrThrow(obj, propertyKey, propertyDesc)。
  12. 返回 empty。
MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 MethodDefinition 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, PropertySetParameterList, FunctionBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 MakeMethod(closure, obj)。
  7. 执行 SetFunctionName(closure, propertyKey, "set")。
  8. 如果 propertyKey 是 Private Name,则
    1. 返回 PrivateElement { [[Key]]: propertyKey, [[Kind]]: accessor, [[Getter]]: undefined, [[Setter]]: closure }。
  9. 令 propertyDesc 为 PropertyDescriptor { [[Setter]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }。
  10. 执行 ? DefinePropertyOrThrow(obj, propertyKey, propertyDesc)。
  11. 返回 empty。
GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 GeneratorMethod 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, UniqueFormalParameters, GeneratorBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 MakeMethod(closure, obj)。
  7. 执行 SetFunctionName(closure, propertyKey)。
  8. 令 proto 为 OrdinaryObjectCreate(%GeneratorPrototype%)。
  9. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  10. 返回 ? DefineMethodProperty(obj, propertyKey, closure, enumerable)。
AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncGeneratorMethod 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, UniqueFormalParameters, AsyncGeneratorBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 MakeMethod(closure, obj)。
  7. 执行 SetFunctionName(closure, propertyKey)。
  8. 令 proto 为 OrdinaryObjectCreate(%AsyncGeneratorPrototype%)。
  9. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  10. 返回 ? DefineMethodProperty(obj, propertyKey, closure, enumerable)。
AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 令 propertyKey 为 ? ClassElementName 的 Evaluation。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncMethod 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, UniqueFormalParameters, AsyncFunctionBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 MakeMethod(closure, obj)。
  7. 执行 SetFunctionName(closure, propertyKey)。
  8. 返回 ? DefineMethodProperty(obj, propertyKey, closure, enumerable)。

15.5 生成器函数定义

语法

GeneratorDeclaration[Yield, Await, Default] : function * BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, ~Await] ) { GeneratorBody } [+Default] function * ( FormalParameters[+Yield, ~Await] ) { GeneratorBody } GeneratorExpression : function * BindingIdentifier[+Yield, ~Await]opt ( FormalParameters[+Yield, ~Await] ) { GeneratorBody } GeneratorMethod[Yield, Await] : * ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[+Yield, ~Await] ) { GeneratorBody } GeneratorBody : FunctionBody[+Yield, ~Await] YieldExpression[In, Await] : yield yield [no LineTerminator here] AssignmentExpression[?In, +Yield, ?Await] yield [no LineTerminator here] * AssignmentExpression[?In, +Yield, ?Await] 注 1

紧跟 yield 之后的句法上下文要求使用 InputElementRegExpOrTemplateTail 词法目标。

注 2

YieldExpression 不能在生成器函数的 FormalParameters 中使用,因为作为 FormalParameters 一部分的任何表达式都会在生成的 Generator 进入可恢复状态之前求值。

注 3

与 Generator 相关的抽象操作定义于 27.8.3。

15.5.1 Static Semantics: 提前错误

GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody } GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody } function * ( FormalParameters ) { GeneratorBody } GeneratorExpression : function * BindingIdentifieropt ( FormalParameters ) { GeneratorBody }

15.5.2 Runtime Semantics: EvaluateGeneratorBody

语法制导操作 EvaluateGeneratorBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(抛出完成 或 返回完成)。 它按以下产生式分别定义:

GeneratorBody : FunctionBody
  1. 执行 ? FunctionDeclarationInstantiation(funcObj, argList)。
  2. 令 gen 为 ? OrdinaryCreateFromConstructor(funcObj, "%GeneratorPrototype%", « [[GeneratorState]], [[GeneratorContext]], [[GeneratorBrand]] »)。
  3. 将 gen.[[GeneratorBrand]] 设置为 empty。
  4. 将 gen.[[GeneratorState]] 设置为 suspended-start。
  5. 执行 GeneratorStart(gen, FunctionBody)。
  6. 返回 ReturnCompletion(gen)。

15.5.3 Runtime Semantics: InstantiateGeneratorFunctionObject

语法制导操作 InstantiateGeneratorFunctionObject,接收参数 envRecord (Environment 记录) 和 privateEnv ((PrivateEnvironment 记录 或 null));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 令 name 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 GeneratorDeclaration 匹配的源文本。
  3. 令 closure 为 OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, FormalParameters, GeneratorBody, non-lexical-this, envRecord, privateEnv)。
  4. 执行 SetFunctionName(closure, name)。
  5. 令 proto 为 OrdinaryObjectCreate(%GeneratorPrototype%)。
  6. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  7. 返回 closure。
GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  1. 令 sourceText 为 GeneratorDeclaration 匹配的源文本。
  2. 令 closure 为 OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, FormalParameters, GeneratorBody, non-lexical-this, envRecord, privateEnv)。
  3. 执行 SetFunctionName(closure, "default")。
  4. 令 proto 为 OrdinaryObjectCreate(%GeneratorPrototype%)。
  5. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  6. 返回 closure。
注

匿名 GeneratorDeclaration 只能作为 export default 声明的一部分出现,因此其函数代码始终是严格模式代码。

15.5.4 Runtime Semantics: InstantiateGeneratorFunctionExpression

语法制导操作 InstantiateGeneratorFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

GeneratorExpression : function * ( FormalParameters ) { GeneratorBody }
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 GeneratorExpression 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, FormalParameters, GeneratorBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 SetFunctionName(closure, name)。
  7. 令 proto 为 OrdinaryObjectCreate(%GeneratorPrototype%)。
  8. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  9. 返回 closure。
GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 断言:不存在 name。
  2. 将 name 设置为 BindingIdentifier 的 StringValue。
  3. 令 outerEnv 为正在运行的执行上下文的 LexicalEnvironment。
  4. 令 funcEnv 为 NewDeclarativeEnvironment(outerEnv)。
  5. 执行 ! funcEnv.CreateImmutableBinding(name, false)。
  6. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  7. 令 sourceText 为 GeneratorExpression 匹配的源文本。
  8. 令 closure 为 OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, FormalParameters, GeneratorBody, non-lexical-this, funcEnv, privateEnv)。
  9. 执行 SetFunctionName(closure, name)。
  10. 令 proto 为 OrdinaryObjectCreate(%GeneratorPrototype%)。
  11. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  12. 执行 ! funcEnv.InitializeBinding(name, closure)。
  13. 返回 closure。
注

GeneratorExpression 中的 BindingIdentifier 可以从 GeneratorExpression 的 FunctionBody 内部引用,从而允许生成器代码递归调用自身。但是,与 GeneratorDeclaration 不同,GeneratorExpression 中的 BindingIdentifier 无法从包围该 GeneratorExpression 的作用域中引用,也不会影响该作用域。

15.5.5 Runtime Semantics: Evaluation

GeneratorExpression : function * BindingIdentifieropt ( FormalParameters ) { GeneratorBody }
  1. 返回 GeneratorExpression 的 InstantiateGeneratorFunctionExpression。
YieldExpression : yield
  1. 返回 ? Yield(undefined)。
YieldExpression : yield AssignmentExpression
  1. 令 exprRef 为 ? AssignmentExpression 的 Evaluation。
  2. 令 value 为 ? GetValue(exprRef)。
  3. 返回 ? Yield(value)。
YieldExpression : yield * AssignmentExpression
  1. 令 genKind 为 GetGeneratorKind()。
  2. 断言:genKind 是 sync 或 async。
  3. 令 exprRef 为 ? AssignmentExpression 的 Evaluation。
  4. 令 value 为 ? GetValue(exprRef)。
  5. 令 iteratorRecord 为 ? GetIterator(value, genKind)。
  6. 令 iterator 为 iteratorRecord.[[Iterator]]。
  7. 令 received 为 NormalCompletion(undefined)。
  8. 重复执行,
    1. 如果 received 是正常完成,则
      1. 令 innerResult 为 ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « received.[[Value]] »)。
      2. 如果 genKind 是 async,则将 innerResult 设置为 ? Await(innerResult)。
      3. 如果 innerResult 不是 Object,则抛出 TypeError 异常。
      4. 令 done 为 ? IteratorComplete(innerResult)。
      5. 如果 done 是 true,则
        1. 返回 ? IteratorValue(innerResult)。
      6. 如果 genKind 是 async,则将 received 设置为 Completion(AsyncGeneratorYield(? IteratorValue(innerResult)))。
      7. 否则,将 received 设置为 Completion(GeneratorYield(innerResult))。
    2. 否则如果 received 是抛出完成,则
      1. 令 throw 为 ? GetMethod(iterator, "throw")。
      2. 如果 throw 不是 undefined,则
        1. 令 innerResult 为 ? Call(throw, iterator, « received.[[Value]] »)。
        2. 如果 genKind 是 async,则将 innerResult 设置为 ? Await(innerResult)。
        3. 注:来自内部迭代器 throw 方法的异常会被传播。内部 throw 方法的正常完成会以与内部 next 类似的方式处理。
        4. 如果 innerResult 不是 Object,则抛出 TypeError 异常。
        5. 令 done 为 ? IteratorComplete(innerResult)。
        6. 如果 done 是 true,则
          1. 返回 ? IteratorValue(innerResult)。
        7. 如果 genKind 是 async,则将 received 设置为 Completion(AsyncGeneratorYield(? IteratorValue(innerResult)))。
        8. 否则,将 received 设置为 Completion(GeneratorYield(innerResult))。
      3. 否则,
        1. 注:如果 iterator 没有 throw 方法,则此抛出将终止 yield* 循环。但首先需要给 iterator 清理的机会。
        2. 令 closeCompletion 为 NormalCompletion(empty)。
        3. 如果 genKind 是 async,则执行 ? AsyncIteratorClose(iteratorRecord, closeCompletion)。
        4. 否则,执行 ? IteratorClose(iteratorRecord, closeCompletion)。
        5. 注:下一步抛出 TypeError,以表示发生了 yield* 协议违反:iterator 没有 throw 方法。
        6. 抛出 TypeError 异常。
    3. 否则,
      1. 断言:received 是 return 完成。
      2. 令 return 为 ? GetMethod(iterator, "return")。
      3. 如果 return 是 undefined,则
        1. 令 receivedValue 为 received.[[Value]]。
        2. 如果 genKind 是 async,则
          1. 将 receivedValue 设置为 ? Await(receivedValue)。
        3. 返回 ReturnCompletion(receivedValue)。
      4. 令 innerReturnResult 为 ? Call(return, iterator, « received.[[Value]] »)。
      5. 如果 genKind 是 async,则将 innerReturnResult 设置为 ? Await(innerReturnResult)。
      6. 如果 innerReturnResult 不是 Object,则抛出 TypeError 异常。
      7. 令 done 为 ? IteratorComplete(innerReturnResult)。
      8. 如果 done 是 true,则
        1. 令 returnedValue 为 ? IteratorValue(innerReturnResult)。
        2. 返回 ReturnCompletion(returnedValue)。
      9. 如果 genKind 是 async,则将 received 设置为 Completion(AsyncGeneratorYield(? IteratorValue(innerReturnResult)))。
      10. 否则,将 received 设置为 Completion(GeneratorYield(innerReturnResult))。

15.6 异步生成器函数定义

语法

AsyncGeneratorDeclaration[Yield, Await, Default] : async [no LineTerminator here] function * BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } [+Default] async [no LineTerminator here] function * ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncGeneratorExpression : async [no LineTerminator here] function * BindingIdentifier[+Yield, +Await]opt ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncGeneratorMethod[Yield, Await] : async [no LineTerminator here] * ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncGeneratorBody : FunctionBody[+Yield, +Await] 注 1

YieldExpression 和 AwaitExpression 不能在异步生成器函数的 FormalParameters 中使用,因为作为 FormalParameters 一部分的任何表达式都会在生成的 AsyncGenerator 进入可恢复状态之前求值。

注 2

与 AsyncGenerator 相关的抽象操作定义于 27.9.3。

15.6.1 Static Semantics: 提前错误

AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody } async function * ( FormalParameters ) { AsyncGeneratorBody } AsyncGeneratorExpression : async function * BindingIdentifieropt ( FormalParameters ) { AsyncGeneratorBody }

15.6.2 Runtime Semantics: EvaluateAsyncGeneratorBody

语法制导操作 EvaluateAsyncGeneratorBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(抛出完成 或 返回完成)。 它按以下产生式分别定义:

AsyncGeneratorBody : FunctionBody
  1. 执行 ? FunctionDeclarationInstantiation(funcObj, argList)。
  2. 令 gen 为 ? OrdinaryCreateFromConstructor(funcObj, "%AsyncGeneratorPrototype%", « [[AsyncGeneratorState]], [[AsyncGeneratorContext]], [[AsyncGeneratorQueue]], [[GeneratorBrand]] »)。
  3. 将 gen.[[GeneratorBrand]] 设置为 empty。
  4. 将 gen.[[AsyncGeneratorState]] 设置为 suspended-start。
  5. 执行 AsyncGeneratorStart(gen, FunctionBody)。
  6. 返回 ReturnCompletion(gen)。

15.6.3 Runtime Semantics: InstantiateAsyncGeneratorFunctionObject

语法制导操作 InstantiateAsyncGeneratorFunctionObject,接收参数 envRecord (Environment 记录) 和 privateEnv ((PrivateEnvironment 记录 或 null));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody }
  1. 令 name 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 AsyncGeneratorDeclaration 匹配的源文本。
  3. 令 closure 为 OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, FormalParameters, AsyncGeneratorBody, non-lexical-this, envRecord, privateEnv)。
  4. 执行 SetFunctionName(closure, name)。
  5. 令 proto 为 OrdinaryObjectCreate(%AsyncGeneratorPrototype%)。
  6. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  7. 返回 closure。
AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 令 sourceText 为 AsyncGeneratorDeclaration 匹配的源文本。
  2. 令 closure 为 OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, FormalParameters, AsyncGeneratorBody, non-lexical-this, envRecord, privateEnv)。
  3. 执行 SetFunctionName(closure, "default")。
  4. 令 proto 为 OrdinaryObjectCreate(%AsyncGeneratorPrototype%)。
  5. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  6. 返回 closure。
注

匿名 AsyncGeneratorDeclaration 只能作为 export default 声明的一部分出现。

15.6.4 Runtime Semantics: InstantiateAsyncGeneratorFunctionExpression

语法制导操作 InstantiateAsyncGeneratorFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

AsyncGeneratorExpression : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncGeneratorExpression 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, FormalParameters, AsyncGeneratorBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 SetFunctionName(closure, name)。
  7. 令 proto 为 OrdinaryObjectCreate(%AsyncGeneratorPrototype%)。
  8. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  9. 返回 closure。
AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody }
  1. 断言:不存在 name。
  2. 将 name 设置为 BindingIdentifier 的 StringValue。
  3. 令 outerEnv 为正在运行的执行上下文的 LexicalEnvironment。
  4. 令 funcEnv 为 NewDeclarativeEnvironment(outerEnv)。
  5. 执行 ! funcEnv.CreateImmutableBinding(name, false)。
  6. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  7. 令 sourceText 为 AsyncGeneratorExpression 匹配的源文本。
  8. 令 closure 为 OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, FormalParameters, AsyncGeneratorBody, non-lexical-this, funcEnv, privateEnv)。
  9. 执行 SetFunctionName(closure, name)。
  10. 令 proto 为 OrdinaryObjectCreate(%AsyncGeneratorPrototype%)。
  11. 执行 ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: proto, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  12. 执行 ! funcEnv.InitializeBinding(name, closure)。
  13. 返回 closure。
注

AsyncGeneratorExpression 中的 BindingIdentifier 可以从 AsyncGeneratorExpression 的 AsyncGeneratorBody 内部引用,从而允许生成器代码递归调用自身。但是,与 AsyncGeneratorDeclaration 不同,AsyncGeneratorExpression 中的 BindingIdentifier 无法从包围该 AsyncGeneratorExpression 的作用域中引用,也不会影响该作用域。

15.6.5 Runtime Semantics: Evaluation

AsyncGeneratorExpression : async function * BindingIdentifieropt ( FormalParameters ) { AsyncGeneratorBody }
  1. 返回 AsyncGeneratorExpression 的 InstantiateAsyncGeneratorFunctionExpression。

15.7 类定义

语法

ClassDeclaration[Yield, Await, Default] : class BindingIdentifier[?Yield, ?Await] ClassTail[?Yield, ?Await] [+Default] class ClassTail[?Yield, ?Await] ClassExpression[Yield, Await] : class BindingIdentifier[?Yield, ?Await]opt ClassTail[?Yield, ?Await] ClassTail[Yield, Await] : ClassHeritage[?Yield, ?Await]opt { ClassBody[?Yield, ?Await]opt } ClassHeritage[Yield, Await] : extends LeftHandSideExpression[?Yield, ?Await] ClassBody[Yield, Await] : ClassElementList[?Yield, ?Await] ClassElementList[Yield, Await] : ClassElement[?Yield, ?Await] ClassElementList[?Yield, ?Await] ClassElement[?Yield, ?Await] ClassElement[Yield, Await] : MethodDefinition[?Yield, ?Await] static MethodDefinition[?Yield, ?Await] FieldDefinition[?Yield, ?Await] ; static FieldDefinition[?Yield, ?Await] ; ClassStaticBlock ; FieldDefinition[Yield, Await] : ClassElementName[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt ClassElementName[Yield, Await] : PropertyName[?Yield, ?Await] PrivateIdentifier ClassStaticBlock : static { ClassStaticBlockBody } ClassStaticBlockBody : ClassStaticBlockStatementList ClassStaticBlockStatementList : StatementList[~Yield, +Await, ~Return]opt 注

类定义始终是严格模式代码。

15.7.1 Static Semantics: 提前错误

ClassTail : ClassHeritageopt { ClassBody } ClassBody : ClassElementList ClassElement : MethodDefinition ClassElement : static MethodDefinition ClassElement : FieldDefinition ; ClassElement : static FieldDefinition ; FieldDefinition : ClassElementName Initializeropt ClassElementName : PrivateIdentifier ClassStaticBlockBody : ClassStaticBlockStatementList

15.7.2 Static Semantics: ClassElementKind

语法制导操作 ClassElementKind,接收无参数;返回值:(constructor-method、non-constructor-method 或 empty)。 它按以下产生式分别定义:

ClassElement : MethodDefinition
  1. 如果 MethodDefinition 的 PropName 是 "constructor",则返回 constructor-method。
  2. 返回 non-constructor-method。
ClassElement : static MethodDefinition FieldDefinition ; static FieldDefinition ;
  1. 返回 non-constructor-method。
ClassElement : ClassStaticBlock
  1. 返回 non-constructor-method。
ClassElement : ;
  1. 返回 empty。

15.7.3 Static Semantics: ConstructorMethod

语法制导操作 ConstructorMethod,接收无参数;返回值:(a ClassElement Parse Node 或 empty)。 它按以下产生式分别定义:

ClassElementList : ClassElement
  1. 如果 ClassElement 的 ClassElementKind 是 constructor-method,则返回 ClassElement。
  2. 返回 empty。
ClassElementList : ClassElementList ClassElement
  1. 令 head 为 ClassElementList 的 ConstructorMethod。
  2. 如果 head 不是 empty,则返回 head。
  3. 如果 ClassElement 的 ClassElementKind 是 constructor-method,则返回 ClassElement。
  4. 返回 empty。
注

提前错误规则确保只有一个名为 "constructor" 的方法定义,并且它既不是访问器属性,也不是生成器定义。

15.7.4 Static Semantics: IsStatic

语法制导操作 IsStatic,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

ClassElement : MethodDefinition
  1. 返回 false。
ClassElement : static MethodDefinition
  1. 返回 true。
ClassElement : FieldDefinition ;
  1. 返回 false。
ClassElement : static FieldDefinition ;
  1. 返回 true。
ClassElement : ClassStaticBlock
  1. 返回 true。
ClassElement : ;
  1. 返回 false。

15.7.5 Static Semantics: NonConstructorElements

语法制导操作 NonConstructorElements,接收无参数;返回值:列表 (元素:ClassElement Parse Nodes)。 它按以下产生式分别定义:

ClassElementList : ClassElement
  1. 如果 ClassElement 的 ClassElementKind 是 non-constructor-method,则
    1. 返回 « ClassElement »。
  2. 返回新的空 List。
ClassElementList : ClassElementList ClassElement
  1. 令 list 为 ClassElementList 的 NonConstructorElements。
  2. 如果 ClassElement 的 ClassElementKind 是 non-constructor-method,则
    1. 将 ClassElement 追加到 list。
  3. 返回 list。

15.7.6 Static Semantics: PrototypePropertyNameList

语法制导操作 PrototypePropertyNameList,接收无参数;返回值:列表 (元素:属性键)。 它按以下产生式分别定义:

ClassElementList : ClassElement
  1. 令 propertyName 为 ClassElement 的 PropName。
  2. 如果 propertyName 是 empty,则返回新的空 List。
  3. 如果 ClassElement 的 IsStatic 是 true,则返回新的空 List。
  4. 返回 « propertyName »。
ClassElementList : ClassElementList ClassElement
  1. 令 list 为 ClassElementList 的 PrototypePropertyNameList。
  2. 令 propertyName 为 ClassElement 的 PropName。
  3. 如果 propertyName 是 empty,则返回 list。
  4. 如果 ClassElement 的 IsStatic 是 true,则返回 list。
  5. 返回 list 和 « propertyName » 的列表连接。

15.7.7 Static Semantics: AllPrivateIdentifiersValid

语法制导操作 AllPrivateIdentifiersValid,接收参数 names (列表 (元素:String));返回值:Boolean。

本规范中未在下方列出的每个文法产生式替代项,都隐式具有以下 AllPrivateIdentifiersValid 默认定义:

  1. 对此 Parse Node 的每个子节点 child,执行
    1. 如果 child 是某个非终结符的实例,则
      1. 如果以 names 为参数对 child 执行 AllPrivateIdentifiersValid 的结果是 false,则返回 false。
  2. 返回 true。
MemberExpression : MemberExpression . PrivateIdentifier
  1. 如果 names 包含 PrivateIdentifier 的 StringValue,则
    1. 返回以 names 为参数对 MemberExpression 执行 AllPrivateIdentifiersValid 的结果。
  2. 返回 false。
CallExpression : CallExpression . PrivateIdentifier
  1. 如果 names 包含 PrivateIdentifier 的 StringValue,则
    1. 返回以 names 为参数对 CallExpression 执行 AllPrivateIdentifiersValid 的结果。
  2. 返回 false。
OptionalChain : ?. PrivateIdentifier
  1. 如果 names 包含 PrivateIdentifier 的 StringValue,则返回 true。
  2. 返回 false。
OptionalChain : OptionalChain . PrivateIdentifier
  1. 如果 names 包含 PrivateIdentifier 的 StringValue,则
    1. 返回以 names 为参数对 OptionalChain 执行 AllPrivateIdentifiersValid 的结果。
  2. 返回 false。
ClassBody : ClassElementList
  1. 令 newNames 为 names 与 ClassBody 的 PrivateBoundIdentifiers 的列表连接。
  2. 返回以 newNames 为参数对 ClassElementList 执行 AllPrivateIdentifiersValid 的结果。
RelationalExpression : PrivateIdentifier in ShiftExpression
  1. 如果 names 包含 PrivateIdentifier 的 StringValue,则
    1. 返回以 names 为参数对 ShiftExpression 执行 AllPrivateIdentifiersValid 的结果。
  2. 返回 false。

15.7.8 Static Semantics: PrivateBoundIdentifiers

语法制导操作 PrivateBoundIdentifiers,接收无参数;返回值:列表 (元素:String)。 它按以下产生式分别定义:

FieldDefinition : ClassElementName Initializeropt
  1. 返回 ClassElementName 的 PrivateBoundIdentifiers。
ClassElementName : PrivateIdentifier
  1. 返回一个 List,其唯一元素是 PrivateIdentifier 的 StringValue。
ClassElementName : PropertyName ClassElement : ClassStaticBlock ;
  1. 返回新的空 List。
ClassElementList : ClassElementList ClassElement
  1. 令 names1 为 ClassElementList 的 PrivateBoundIdentifiers。
  2. 令 names2 为 ClassElement 的 PrivateBoundIdentifiers。
  3. 返回 names1 和 names2 的列表连接。
MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } get ClassElementName ( ) { FunctionBody } set ClassElementName ( PropertySetParameterList ) { FunctionBody } GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody } AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 返回 ClassElementName 的 PrivateBoundIdentifiers。

15.7.9 Static Semantics: ContainsArguments

语法制导操作 ContainsArguments,接收无参数;返回值:Boolean。

本规范中未在下方列出的每个文法产生式替代项,都隐式具有以下 ContainsArguments 默认定义:

  1. 对此 Parse Node 的每个子节点 child,执行
    1. 如果 child 是某个非终结符的实例,则
      1. 如果 child 的 ContainsArguments 是 true,则返回 true。
  2. 返回 false。
IdentifierReference : Identifier
  1. 如果 Identifier 的 StringValue 是 "arguments",则返回 true。
  2. 返回 false。
FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody } function ( FormalParameters ) { FunctionBody } FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody } GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody } function * ( FormalParameters ) { GeneratorBody } GeneratorExpression : function * BindingIdentifieropt ( FormalParameters ) { GeneratorBody } AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody } async function * ( FormalParameters ) { AsyncGeneratorBody } AsyncGeneratorExpression : async function * BindingIdentifieropt ( FormalParameters ) { AsyncGeneratorBody } AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody } async function ( FormalParameters ) { AsyncFunctionBody } AsyncFunctionExpression : async function BindingIdentifieropt ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 false。
MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } get ClassElementName ( ) { FunctionBody } set ClassElementName ( PropertySetParameterList ) { FunctionBody } GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody } AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 返回 ClassElementName 的 ContainsArguments。

15.7.10 Runtime Semantics: ClassFieldDefinitionEvaluation

语法制导操作 ClassFieldDefinitionEvaluation,接收参数 homeObj (Object);返回值:(正常完成 (值:ClassFieldDefinition 记录) 或 突兀完成)。 它按以下产生式分别定义:

FieldDefinition : ClassElementName Initializeropt
  1. 令 name 为 ? ClassElementName 的 Evaluation。
  2. 如果存在 Initializer,则
    1. 令 formalParamList 为产生式 FormalParameters : [empty] 的一个实例。
    2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
    3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
    4. 令 sourceText 为空 Unicode 码点序列。
    5. 令 initializer 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParamList, Initializer, non-lexical-this, envRecord, privateEnv)。
    6. 执行 MakeMethod(initializer, homeObj)。
    7. 将 initializer.[[ClassFieldInitializerName]] 设置为 name。
  3. 否则,
    1. 令 initializer 为 empty。
  4. 返回 ClassFieldDefinition Record { [[Name]]: name, [[Initializer]]: initializer }。
注
为 initializer 创建的函数永远无法被 ECMAScript 代码直接访问。

15.7.11 Runtime Semantics: ClassStaticBlockDefinitionEvaluation

语法制导操作 ClassStaticBlockDefinitionEvaluation,接收参数 homeObj (Object);返回值:ClassStaticBlockDefinition 记录。 它按以下产生式分别定义:

ClassStaticBlock : static { ClassStaticBlockBody }
  1. 令 lexicalEnv 为正在运行的执行上下文的 LexicalEnvironment。
  2. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  3. 令 sourceText 为空 Unicode 码点序列。
  4. 令 formalParams 为产生式 FormalParameters : [empty] 的一个实例。
  5. 令 bodyFunc 为 OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParams, ClassStaticBlockBody, non-lexical-this, lexicalEnv, privateEnv)。
  6. 执行 MakeMethod(bodyFunc, homeObj)。
  7. 返回 ClassStaticBlockDefinition Record { [[BodyFunction]]: bodyFunc }。
注
函数 bodyFunc 永远无法被 ECMAScript 代码直接访问。

15.7.12 Runtime Semantics: EvaluateClassStaticBlockBody

语法制导操作 EvaluateClassStaticBlockBody,接收参数 funcObj (ECMAScript 函数对象);返回值:(返回完成 或 抛出完成)。 它按以下产生式分别定义:

ClassStaticBlockBody : ClassStaticBlockStatementList
  1. 断言:funcObj 是由 ClassStaticBlockDefinitionEvaluation 的步骤 5 创建的合成函数。
  2. 执行 ! FunctionDeclarationInstantiation(funcObj, « »)。
  3. 令 result 为 Completion(ClassStaticBlockStatementList 的 Evaluation)。
  4. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  5. 断言:envRecord 是 Declarative Environment Record。
  6. 执行 ? DisposeResources(envRecord.[[DisposableResourceStack]], result)。
  7. 返回 ReturnCompletion(undefined)。

15.7.13 Runtime Semantics: ClassElementEvaluation

语法制导操作 ClassElementEvaluation,接收参数 obj (Object);返回值:(正常完成 (值:(ClassFieldDefinition 记录、ClassStaticBlockDefinition 记录、PrivateElement 或 empty)) 或 突兀完成)。 它按以下产生式分别定义:

ClassElement : FieldDefinition ; static FieldDefinition ;
  1. 返回 ? 以 obj 为参数对 FieldDefinition 执行 ClassFieldDefinitionEvaluation 的结果。
ClassElement : MethodDefinition static MethodDefinition
  1. 返回 ? 以 obj 和 false 为参数对 MethodDefinition 执行 MethodDefinitionEvaluation 的结果。
ClassElement : ClassStaticBlock
  1. 返回以 obj 为参数对 ClassStaticBlock 执行 ClassStaticBlockDefinitionEvaluation 的结果。
ClassElement : ;
  1. 返回 empty。

15.7.14 Runtime Semantics: ClassDefinitionEvaluation

语法制导操作 ClassDefinitionEvaluation,接收参数 classBinding ((String 或 undefined))、className ((属性键 或 私有名称)) 和 sourceText (ECMAScript 源文本);返回值:(正常完成 (值:函数对象) 或 突兀完成)。

注

为了便于规范描述,私有方法和访问器与私有字段一起包含在类实例的 [[PrivateElements]] 槽中。不过,对于给定类定义的私有方法和访问器,任意给定对象要么全部具有,要么一个也没有。此特性的设计允许实现选择一种不需要单独跟踪每个方法或访问器的策略来实现私有方法和访问器。

例如,实现可以直接将实例私有方法与其对应的 Private Name 关联,并针对每个对象跟踪哪些类构造函数曾以该对象作为其 this 值运行。随后,在对象上查找实例私有方法时,只需检查定义该方法的类构造函数是否已用于初始化该对象,然后返回与 Private Name 关联的方法。

这与私有字段不同:因为字段初始化器可能在类实例化期间抛出异常,所以单个对象可能只具有给定类私有字段的某个真子集,因此通常必须单独跟踪私有字段。

它按以下产生式分别定义:

ClassTail : ClassHeritageopt { ClassBodyopt }
  1. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  2. 令 classEnv 为 NewDeclarativeEnvironment(envRecord)。
  3. 如果 classBinding 不是 undefined,则
    1. 执行 ! classEnv.CreateImmutableBinding(classBinding, true)。
  4. 令 outerPrivateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  5. 令 classPrivateEnv 为 NewPrivateEnvironment(outerPrivateEnv)。
  6. 如果存在 ClassBody,则
    1. 对 ClassBody 的 PrivateBoundIdentifiers 的每个 String description,执行
      1. 如果 classPrivateEnv.[[Names]] 包含一个 Private Name privateName,使得 privateName.[[Description]] 是 description,则
        1. 断言:这种情况只可能发生在 getter/setter 对上。
      2. 否则,
        1. 令 name 为一个新的 Private Name,其 [[Description]] 为 description。
        2. 将 name 追加到 classPrivateEnv.[[Names]]。
  7. 如果不存在 ClassHeritage,则
    1. 令 protoParent 为 %Object.prototype%。
    2. 令 ctorParent 为 %Function.prototype%。
  8. 否则,
    1. 将正在运行的执行上下文的 LexicalEnvironment 设置为 classEnv。
    2. 注:对 ClassHeritage 求值时,正在运行的执行上下文的 PrivateEnvironment 是 outerPrivateEnv。
    3. 令 superclassRef 为 Completion(ClassHeritage 的 Evaluation)。
    4. 将正在运行的执行上下文的 LexicalEnvironment 设置为 envRecord。
    5. 令 superclass 为 ? GetValue(? superclassRef)。
    6. 如果 superclass 是 null,则
      1. 令 protoParent 为 null。
      2. 令 ctorParent 为 %Function.prototype%。
    7. 否则如果 IsConstructor(superclass) 是 false,则
      1. 抛出 TypeError 异常。
    8. 否则,
      1. 令 protoParent 为 ? Get(superclass, "prototype")。
      2. 如果 protoParent 不是 Object 且 protoParent 不是 null,则抛出 TypeError 异常。
      3. 令 ctorParent 为 superclass。
  9. 令 proto 为 OrdinaryObjectCreate(protoParent)。
  10. 如果不存在 ClassBody,则令 ctor 为 empty。
  11. 否则,令 ctor 为 ClassBody 的 ConstructorMethod。
  12. 将正在运行的执行上下文的 LexicalEnvironment 设置为 classEnv。
  13. 将正在运行的执行上下文的 PrivateEnvironment 设置为 classPrivateEnv。
  14. 如果 ctor 是 empty,则
    1. 令 defaultCtor 为一个没有参数、且不捕获任何内容的新 Abstract Closure,它在被调用时执行以下步骤:
      1. 令 args 为通过 [[Call]] 或 [[Construct]] 传给此函数的参数 List。
      2. 如果 NewTarget 是 undefined,则抛出 TypeError 异常。
      3. 令 ctorFunc 为活动函数对象。
      4. 如果 ctorFunc.[[ConstructorKind]] 是 derived,则
        1. 注:此分支的行为类似于 constructor(...args) { super(...args); }。最显著的区别是,前述 ECMAScript 源文本会以可观察方式调用 %Array.prototype% 上的 %Symbol.iterator% 方法,而此函数不会。
        2. 令 func 为 ! ctorFunc.[[GetPrototypeOf]]()。
        3. 如果 IsConstructor(func) 是 false,则抛出 TypeError 异常。
        4. 令 result 为 ? Construct(func, args, NewTarget)。
      5. 否则,
        1. 注:此分支的行为类似于 constructor() {}。
        2. 令 result 为 ? OrdinaryCreateFromConstructor(NewTarget, "%Object.prototype%")。
      6. 执行 ? InitializeInstanceElements(result, ctorFunc)。
      7. 返回 NormalCompletion(result)。
    2. 令 ctorFunc 为 CreateBuiltinFunction(defaultCtor, 0, className, « [[ConstructorKind]], [[SourceText]], [[PrivateMethods]], [[Fields]] », 当前 Realm Record, ctorParent)。
  15. 否则,
    1. 令 ctorInfo 为 ! 以 proto 和 ctorParent 为参数对 ctor 执行 DefineMethod 的结果。
    2. 令 ctorFunc 为 ctorInfo.[[Closure]]。
    3. 执行 MakeClassConstructor(ctorFunc)。
    4. 执行 SetFunctionName(ctorFunc, className)。
  16. 将 ctorFunc.[[SourceText]] 设置为 sourceText。
  17. 执行 MakeConstructor(ctorFunc, false, proto)。
  18. 如果存在 ClassHeritage,则将 ctorFunc.[[ConstructorKind]] 设置为 derived。
  19. 执行 ! DefineMethodProperty(proto, "constructor", ctorFunc, false)。
  20. 如果不存在 ClassBody,则令 classElements 为新的空 List。
  21. 否则,令 classElements 为 ClassBody 的 NonConstructorElements。
  22. 令 instancePrivateMethods 为新的空 List。
  23. 令 staticPrivateMethods 为新的空 List。
  24. 令 instanceFields 为新的空 List。
  25. 令 staticElements 为新的空 List。
  26. 对 classElements 的每个 ClassElement classElement,执行
    1. 如果 classElement 的 IsStatic 是 false,则
      1. 令 element 为 Completion(以 proto 为参数对 classElement 执行 ClassElementEvaluation 的结果)。
    2. 否则,
      1. 令 element 为 Completion(以 ctorFunc 为参数对 classElement 执行 ClassElementEvaluation 的结果)。
    3. 如果 element 是突然完成,则
      1. 将正在运行的执行上下文的 LexicalEnvironment 设置为 envRecord。
      2. 将正在运行的执行上下文的 PrivateEnvironment 设置为 outerPrivateEnv。
      3. 返回 ? element。
    4. 将 element 设置为 ! element。
    5. 如果 element 是 PrivateElement,则
      1. 断言:element.[[Kind]] 是 method 或 accessor。
      2. 如果 classElement 的 IsStatic 是 false,则令 container 为 instancePrivateMethods。
      3. 否则,令 container 为 staticPrivateMethods。
      4. 如果 container 包含一个 PrivateElement existingElement,使得 existingElement.[[Key]] 是 element.[[Key]],则
        1. 断言:element.[[Kind]] 和 existingElement.[[Kind]] 都是 accessor。
        2. 如果 element.[[Getter]] 是 undefined,则
          1. 令 combined 为 PrivateElement { [[Key]]: element.[[Key]], [[Kind]]: accessor, [[Getter]]: existingElement.[[Getter]], [[Setter]]: element.[[Setter]] }。
        3. 否则,
          1. 令 combined 为 PrivateElement { [[Key]]: element.[[Key]], [[Kind]]: accessor, [[Getter]]: element.[[Getter]], [[Setter]]: existingElement.[[Setter]] }。
        4. 用 combined 替换 container 中的 existingElement。
      5. 否则,
        1. 将 element 追加到 container。
    6. 否则如果 element 是 ClassFieldDefinition Record,则
      1. 如果 classElement 的 IsStatic 是 false,则将 element 追加到 instanceFields。
      2. 否则,将 element 追加到 staticElements。
    7. 否则如果 element 是 ClassStaticBlockDefinition Record,则
      1. 将 element 追加到 staticElements。
    8. 否则,
      1. 断言:element 是 empty。
  27. 将正在运行的执行上下文的 LexicalEnvironment 设置为 envRecord。
  28. 如果 classBinding 不是 undefined,则
    1. 执行 ! classEnv.InitializeBinding(classBinding, ctorFunc)。
  29. 将 ctorFunc.[[PrivateMethods]] 设置为 instancePrivateMethods。
  30. 将 ctorFunc.[[Fields]] 设置为 instanceFields。
  31. 对 staticPrivateMethods 的每个 PrivateElement method,执行
    1. 执行 ! PrivateMethodOrAccessorAdd(ctorFunc, method)。
  32. 对 staticElements 的每个元素 elementRecord,执行
    1. 如果 elementRecord 是 ClassFieldDefinition Record,则
      1. 令 result 为 Completion(DefineField(ctorFunc, elementRecord))。
    2. 否则,
      1. 断言:elementRecord 是 ClassStaticBlockDefinition Record。
      2. 令 result 为 Completion(Call(elementRecord.[[BodyFunction]], ctorFunc))。
    3. 如果 result 是突然完成,则
      1. 将正在运行的执行上下文的 PrivateEnvironment 设置为 outerPrivateEnv。
      2. 返回 ? result。
  33. 将正在运行的执行上下文的 PrivateEnvironment 设置为 outerPrivateEnv。
  34. 返回 ctorFunc。

15.7.15 Runtime Semantics: BindingClassDeclarationEvaluation

语法制导操作 BindingClassDeclarationEvaluation,接收无参数;返回值:(正常完成 (值:函数对象) 或 突兀完成)。 它按以下产生式分别定义:

ClassDeclaration : class BindingIdentifier ClassTail
  1. 令 className 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 ClassDeclaration 匹配的源文本。
  3. 令 value 为 ? 以 className、className 和 sourceText 为参数对 ClassTail 执行 ClassDefinitionEvaluation 的结果。
  4. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  5. 执行 ? InitializeBoundName(className, value, envRecord)。
  6. 返回 value。
ClassDeclaration : class ClassTail
  1. 令 sourceText 为 ClassDeclaration 匹配的源文本。
  2. 返回 ? 以 undefined、"default" 和 sourceText 为参数对 ClassTail 执行 ClassDefinitionEvaluation 的结果。
注

ClassDeclaration : class ClassTail 仅作为 ExportDeclaration 的一部分出现,并且建立其绑定的操作作为该产生式求值操作的一部分处理。参见 16.2.3.7。

15.7.16 Runtime Semantics: Evaluation

ClassDeclaration : class BindingIdentifier ClassTail
  1. 执行 ? 当前 ClassDeclaration 的 BindingClassDeclarationEvaluation。
  2. 返回 empty。
注

ClassDeclaration : class ClassTail 仅作为 ExportDeclaration 的一部分出现,绝不会被直接求值。

ClassExpression : class ClassTail
  1. 令 sourceText 为 ClassExpression 匹配的源文本。
  2. 返回 ? 以 undefined、空 String 和 sourceText 为参数对 ClassTail 执行 ClassDefinitionEvaluation 的结果。
ClassExpression : class BindingIdentifier ClassTail
  1. 令 className 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 ClassExpression 匹配的源文本。
  3. 返回 ? 以 className、className 和 sourceText 为参数对 ClassTail 执行 ClassDefinitionEvaluation 的结果。
ClassElementName : PrivateIdentifier
  1. 令 privateIdentifier 为 PrivateIdentifier 的 StringValue。
  2. 令 privateEnvRecord 为正在运行的执行上下文的 PrivateEnvironment。
  3. 令 names 为 privateEnvRecord.[[Names]]。
  4. 断言:names 中恰好有一个元素是 [[Description]] 为 privateIdentifier 的 Private Name。
  5. 令 privateName 为 names 中 [[Description]] 为 privateIdentifier 的 Private Name。
  6. 返回 privateName。
ClassStaticBlockStatementList : [empty]
  1. 返回 undefined。

15.8 异步函数定义

语法

AsyncFunctionDeclaration[Yield, Await, Default] : async [no LineTerminator here] function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } [+Default] async [no LineTerminator here] function ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncFunctionExpression : async [no LineTerminator here] function BindingIdentifier[~Yield, +Await]opt ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncMethod[Yield, Await] : async [no LineTerminator here] ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncFunctionBody : FunctionBody[~Yield, +Await] AwaitExpression[Yield] : await UnaryExpression[?Yield, +Await] 注 1

当存在 [Await] 参数时,await 会被解析为 AwaitExpression 的关键字。以下上下文的顶层存在 [Await] 参数,不过取决于 FunctionBody 等非终结符,在某些上下文中该参数可能不存在:

当 Script 是句法目标符号时,如果不存在 [Await] 参数,则 await 可以被解析为标识符。这包括以下上下文:

注 2

与 YieldExpression 不同,省略 AwaitExpression 的操作数是语法错误。必须等待某个值。

15.8.1 Static Semantics: 提前错误

AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody } async function ( FormalParameters ) { AsyncFunctionBody } AsyncFunctionExpression : async function BindingIdentifieropt ( FormalParameters ) { AsyncFunctionBody }

15.8.2 Runtime Semantics: InstantiateAsyncFunctionObject

语法制导操作 InstantiateAsyncFunctionObject,接收参数 envRecord (Environment 记录) 和 privateEnv ((PrivateEnvironment 记录 或 null));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 令 name 为 BindingIdentifier 的 StringValue。
  2. 令 sourceText 为 AsyncFunctionDeclaration 匹配的源文本。
  3. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, envRecord, privateEnv)。
  4. 执行 SetFunctionName(closure, name)。
  5. 返回 closure。
AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 令 sourceText 为 AsyncFunctionDeclaration 匹配的源文本。
  2. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, envRecord, privateEnv)。
  3. 执行 SetFunctionName(closure, "default")。
  4. 返回 closure。

15.8.3 Runtime Semantics: InstantiateAsyncFunctionExpression

语法制导操作 InstantiateAsyncFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncFunctionExpression 匹配的源文本。
  5. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, envRecord, privateEnv)。
  6. 执行 SetFunctionName(closure, name)。
  7. 返回 closure。
AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 断言:不存在 name。
  2. 将 name 设置为 BindingIdentifier 的 StringValue。
  3. 令 outerEnv 为正在运行的执行上下文的 LexicalEnvironment。
  4. 令 funcEnv 为 NewDeclarativeEnvironment(outerEnv)。
  5. 执行 ! funcEnv.CreateImmutableBinding(name, false)。
  6. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  7. 令 sourceText 为 AsyncFunctionExpression 匹配的源文本。
  8. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, funcEnv, privateEnv)。
  9. 执行 SetFunctionName(closure, name)。
  10. 执行 ! funcEnv.InitializeBinding(name, closure)。
  11. 返回 closure。
注

AsyncFunctionExpression 中的 BindingIdentifier 可以从 AsyncFunctionExpression 的 AsyncFunctionBody 内部引用,从而允许函数递归调用自身。但是,与 FunctionDeclaration 不同,AsyncFunctionExpression 中的 BindingIdentifier 无法从包围该 AsyncFunctionExpression 的作用域中引用,也不会影响该作用域。

15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody

语法制导操作 EvaluateAsyncFunctionBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:返回完成。 它按以下产生式分别定义:

AsyncFunctionBody : FunctionBody
  1. 令 promiseCapability 为 ! NewPromiseCapability(%Promise%)。
  2. 令 completion 为 Completion(FunctionDeclarationInstantiation(funcObj, argList))。
  3. 如果 completion 是突然完成,则
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « completion.[[Value]] »)。
  4. 否则,
    1. 执行 AsyncFunctionStart(promiseCapability, FunctionBody)。
  5. 返回 ReturnCompletion(promiseCapability.[[Promise]])。

15.8.5 Runtime Semantics: Evaluation

AsyncFunctionExpression : async function BindingIdentifieropt ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 AsyncFunctionExpression 的 InstantiateAsyncFunctionExpression。
UnaryExpression : CoverAwaitExpressionAndAwaitUsingDeclarationHead
  1. 令 expr 为 CoverAwaitExpressionAndAwaitUsingDeclarationHead 所覆盖的 AwaitExpression。
  2. 返回 ? expr 的 Evaluation。
AwaitExpression : await UnaryExpression
  1. 令 exprRef 为 ? UnaryExpression 的 Evaluation。
  2. 令 value 为 ? GetValue(exprRef)。
  3. 返回 ? Await(value)。

15.9 异步箭头函数定义

语法

AsyncArrowFunction[In, Yield, Await] : async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In] CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] [no LineTerminator here] => AsyncConciseBody[?In] AsyncConciseBody[In] : [lookahead ≠ {] ExpressionBody[?In, +Await] { AsyncFunctionBody } AsyncArrowBindingIdentifier[Yield] : BindingIdentifier[?Yield, +Await] CoverCallExpressionAndAsyncArrowHead[Yield, Await] : MemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await]

补充语法

当处理产生式
AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
的实例时,使用以下文法进一步细化对 CoverCallExpressionAndAsyncArrowHead 的解释:

AsyncArrowHead : async [no LineTerminator here] ArrowFormalParameters[~Yield, +Await]

15.9.1 Static Semantics: 提前错误

AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody

15.9.2 Static Semantics: AsyncConciseBodyContainsUseStrict

语法制导操作 AsyncConciseBodyContainsUseStrict,接收无参数;返回值:Boolean。 它按以下产生式分别定义:

AsyncConciseBody : ExpressionBody
  1. 返回 false。
AsyncConciseBody : { AsyncFunctionBody }
  1. 返回 AsyncFunctionBody 的 FunctionBodyContainsUseStrict。

15.9.3 Runtime Semantics: EvaluateAsyncConciseBody

语法制导操作 EvaluateAsyncConciseBody,接收参数 funcObj (ECMAScript 函数对象) 和 argList (列表 (元素:ECMAScript 语言值));返回值:返回完成。 它按以下产生式分别定义:

AsyncConciseBody : ExpressionBody
  1. 令 promiseCapability 为 ! NewPromiseCapability(%Promise%)。
  2. 令 completion 为 Completion(FunctionDeclarationInstantiation(funcObj, argList))。
  3. 如果 completion 是突然完成,则
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « completion.[[Value]] »)。
  4. 否则,
    1. 执行 AsyncFunctionStart(promiseCapability, ExpressionBody)。
  5. 返回 ReturnCompletion(promiseCapability.[[Promise]])。

15.9.4 Runtime Semantics: InstantiateAsyncArrowFunctionExpression

语法制导操作 InstantiateAsyncArrowFunctionExpression,接收可选参数 name ((属性键 或 私有名称));返回值:ECMAScript 函数对象。 它按以下产生式分别定义:

AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncArrowFunction 匹配的源文本。
  5. 令 params 为 AsyncArrowBindingIdentifier。
  6. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, params, AsyncConciseBody, lexical-this, envRecord, privateEnv)。
  7. 执行 SetFunctionName(closure, name)。
  8. 返回 closure。
AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 如果不存在 name,则将 name 设置为空 String。
  2. 令 envRecord 为正在运行的执行上下文的 LexicalEnvironment。
  3. 令 privateEnv 为正在运行的执行上下文的 PrivateEnvironment。
  4. 令 sourceText 为 AsyncArrowFunction 匹配的源文本。
  5. 令 head 为 CoverCallExpressionAndAsyncArrowHead 所覆盖的 AsyncArrowHead。
  6. 令 params 为 head 的 ArrowFormalParameters。
  7. 令 closure 为 OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, params, AsyncConciseBody, lexical-this, envRecord, privateEnv)。
  8. 执行 SetFunctionName(closure, name)。
  9. 返回 closure。

15.9.5 Runtime Semantics: Evaluation

AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 返回 AsyncArrowFunction 的 InstantiateAsyncArrowFunctionExpression。

15.10 尾位置调用

15.10.1 Static Semantics: IsInTailPosition ( call )

抽象操作 IsInTailPosition,接收参数 call ((a CallExpression Parse Node、a MemberExpression Parse Node 或 an OptionalChain Parse Node));返回值:Boolean。 调用时执行以下步骤:

  1. 如果 IsStrict(call) 是 false,则返回 false。
  2. 如果 call 未包含在 FunctionBody、ConciseBody 或 AsyncConciseBody 中,则返回 false。
  3. 令 body 为最紧密包含 call 的 FunctionBody、ConciseBody 或 AsyncConciseBody。
  4. 如果 body 是 GeneratorBody 的 FunctionBody,则返回 false。
  5. 如果 body 是 AsyncFunctionBody 的 FunctionBody,则返回 false。
  6. 如果 body 是 AsyncGeneratorBody 的 FunctionBody,则返回 false。
  7. 如果 body 是 AsyncConciseBody,则返回 false。
  8. 返回以 call 为参数对 body 执行 HasCallInTailPosition 的结果。
注

尾位置调用仅在严格模式代码中定义,这是因为常见的非标准语言扩展(参见 10.2.4)允许观察调用方上下文链。

15.10.2 Static Semantics: HasCallInTailPosition

语法制导操作 HasCallInTailPosition,接收参数 call ((a CallExpression Parse Node、a MemberExpression Parse Node 或 an OptionalChain Parse Node));返回值:Boolean。

注 1

call 是表示特定源文本范围的 Parse Node。当以下算法将 call 与另一个 Parse Node 比较时,测试的是它们是否表示相同的源文本。

注 2

一个潜在的尾位置调用,如果其后紧接着对调用结果执行 return GetValue,也同样是可能的尾位置调用。函数调用无法返回 Reference Record,因此这种 GetValue 操作始终返回与实际函数调用结果相同的值。

注 3

如果 using 声明或 await using 声明位于同一个 Block、ForStatement、ForInOfStatement、FunctionBody、GeneratorBody、AsyncGeneratorBody、AsyncFunctionBody 或 ClassStaticBlockBody 中某次调用之前,则会阻止该调用成为可能的尾位置调用。

它按以下产生式分别定义:

StatementList : StatementList StatementListItem
  1. 令 has 为以 call 为参数对派生的 StatementList 执行 HasCallInTailPosition 的结果。
  2. 如果 has 是 true,则返回 true。
  3. 如果派生的 StatementList 的 ContainsUsing 是 true,则返回 false。
  4. 返回以 call 为参数对 StatementListItem 执行 HasCallInTailPosition 的结果。
FunctionStatementList : [empty] StatementListItem : Declaration Statement : VariableStatement EmptyStatement ExpressionStatement ContinueStatement BreakStatement ThrowStatement DebuggerStatement Block : { } ReturnStatement : return ; LabelledItem : FunctionDeclaration ForInOfStatement : for ( LeftHandSideExpression of AssignmentExpression ) Statement for ( var ForBinding of AssignmentExpression ) Statement for ( ForDeclaration of AssignmentExpression ) Statement CaseBlock : { }
  1. 返回 false。
IfStatement : if ( Expression ) Statement else Statement
  1. 令 has 为以 call 为参数对第一个 Statement 执行 HasCallInTailPosition 的结果。
  2. 如果 has 是 true,则返回 true。
  3. 返回以 call 为参数对第二个 Statement 执行 HasCallInTailPosition 的结果。
IfStatement : if ( Expression ) Statement DoWhileStatement : do Statement while ( Expression ) ; WhileStatement : while ( Expression ) Statement ForStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement ForInOfStatement : for ( LeftHandSideExpression in Expression ) Statement for ( var ForBinding in Expression ) Statement for ( ForDeclaration in Expression ) Statement WithStatement : with ( Expression ) Statement
  1. 返回以 call 为参数对 Statement 执行 HasCallInTailPosition 的结果。
LabelledStatement : LabelIdentifier : LabelledItem
  1. 返回以 call 为参数对 LabelledItem 执行 HasCallInTailPosition 的结果。
ReturnStatement : return Expression ;
  1. 返回以 call 为参数对 Expression 执行 HasCallInTailPosition 的结果。
SwitchStatement : switch ( Expression ) CaseBlock
  1. 返回以 call 为参数对 CaseBlock 执行 HasCallInTailPosition 的结果。
CaseBlock : { CaseClausesopt DefaultClause CaseClausesopt }
  1. 令 has 为 false。
  2. 如果存在第一个 CaseClauses,则将 has 设置为以 call 为参数对第一个 CaseClauses 执行 HasCallInTailPosition 的结果。
  3. 如果 has 是 true,则返回 true。
  4. 将 has 设置为以 call 为参数对 DefaultClause 执行 HasCallInTailPosition 的结果。
  5. 如果 has 是 true,则返回 true。
  6. 如果存在第二个 CaseClauses,则将 has 设置为以 call 为参数对第二个 CaseClauses 执行 HasCallInTailPosition 的结果。
  7. 返回 has。
CaseClauses : CaseClauses CaseClause
  1. 令 has 为以 call 为参数对 CaseClauses 执行 HasCallInTailPosition 的结果。
  2. 如果 has 是 true,则返回 true。
  3. 返回以 call 为参数对 CaseClause 执行 HasCallInTailPosition 的结果。
CaseClause : case Expression : StatementListopt DefaultClause : default : StatementListopt
  1. 如果存在 StatementList,则返回以 call 为参数对 StatementList 执行 HasCallInTailPosition 的结果。
  2. 返回 false。
TryStatement : try Block Catch
  1. 返回以 call 为参数对 Catch 执行 HasCallInTailPosition 的结果。
TryStatement : try Block Finally try Block Catch Finally
  1. 返回以 call 为参数对 Finally 执行 HasCallInTailPosition 的结果。
Catch : catch ( CatchParameter ) Block
  1. 返回以 call 为参数对 Block 执行 HasCallInTailPosition 的结果。
AssignmentExpression : YieldExpression ArrowFunction AsyncArrowFunction LeftHandSideExpression = AssignmentExpression LeftHandSideExpression AssignmentOperator AssignmentExpression LeftHandSideExpression &&= AssignmentExpression LeftHandSideExpression ||= AssignmentExpression LeftHandSideExpression ??= AssignmentExpression BitwiseANDExpression : BitwiseANDExpression & EqualityExpression BitwiseXORExpression : BitwiseXORExpression ^ BitwiseANDExpression BitwiseORExpression : BitwiseORExpression | BitwiseXORExpression EqualityExpression : EqualityExpression == RelationalExpression EqualityExpression != RelationalExpression EqualityExpression === RelationalExpression EqualityExpression !== RelationalExpression RelationalExpression : RelationalExpression < ShiftExpression RelationalExpression > ShiftExpression RelationalExpression <= ShiftExpression RelationalExpression >= ShiftExpression RelationalExpression instanceof ShiftExpression RelationalExpression in ShiftExpression PrivateIdentifier in ShiftExpression ShiftExpression : ShiftExpression << AdditiveExpression ShiftExpression >> AdditiveExpression ShiftExpression >>> AdditiveExpression AdditiveExpression : AdditiveExpression + MultiplicativeExpression AdditiveExpression - MultiplicativeExpression MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression ExponentiationExpression : UpdateExpression ** ExponentiationExpression UpdateExpression : LeftHandSideExpression ++ LeftHandSideExpression -- ++ UnaryExpression -- UnaryExpression UnaryExpression : delete UnaryExpression void UnaryExpression typeof UnaryExpression + UnaryExpression - UnaryExpression ~ UnaryExpression ! UnaryExpression CoverAwaitExpressionAndAwaitUsingDeclarationHead CallExpression : SuperCall ImportCall CallExpression [ Expression ] CallExpression . IdentifierName CallExpression . PrivateIdentifier NewExpression : new NewExpression MemberExpression : MemberExpression [ Expression ] MemberExpression . IdentifierName SuperProperty MetaProperty new MemberExpression Arguments MemberExpression . PrivateIdentifier PrimaryExpression : this IdentifierReference Literal ArrayLiteral ObjectLiteral FunctionExpression ClassExpression GeneratorExpression AsyncFunctionExpression AsyncGeneratorExpression RegularExpressionLiteral TemplateLiteral
  1. 返回 false。
Expression : AssignmentExpression Expression , AssignmentExpression
  1. 返回以 call 为参数对 AssignmentExpression 执行 HasCallInTailPosition 的结果。
ConditionalExpression : ShortCircuitExpression ? AssignmentExpression : AssignmentExpression
  1. 令 has 为以 call 为参数对第一个 AssignmentExpression 执行 HasCallInTailPosition 的结果。
  2. 如果 has 是 true,则返回 true。
  3. 返回以 call 为参数对第二个 AssignmentExpression 执行 HasCallInTailPosition 的结果。
LogicalANDExpression : LogicalANDExpression && BitwiseORExpression
  1. 返回以 call 为参数对 BitwiseORExpression 执行 HasCallInTailPosition 的结果。
LogicalORExpression : LogicalORExpression || LogicalANDExpression
  1. 返回以 call 为参数对 LogicalANDExpression 执行 HasCallInTailPosition 的结果。
CoalesceExpression : CoalesceExpressionHead ?? BitwiseORExpression
  1. 返回以 call 为参数对 BitwiseORExpression 执行 HasCallInTailPosition 的结果。
CallExpression : CoverCallExpressionAndAsyncArrowHead CallExpression Arguments CallExpression TemplateLiteral
  1. 如果此 CallExpression 是 call,则返回 true。
  2. 返回 false。
OptionalExpression : MemberExpression OptionalChain CallExpression OptionalChain OptionalExpression OptionalChain
  1. 返回以 call 为参数对 OptionalChain 执行 HasCallInTailPosition 的结果。
OptionalChain : ?. [ Expression ] ?. IdentifierName ?. PrivateIdentifier OptionalChain [ Expression ] OptionalChain . IdentifierName OptionalChain . PrivateIdentifier
  1. 返回 false。
OptionalChain : ?. Arguments OptionalChain Arguments
  1. 如果此 OptionalChain 是 call,则返回 true。
  2. 返回 false。
MemberExpression : MemberExpression TemplateLiteral
  1. 如果此 MemberExpression 是 call,则返回 true。
  2. 返回 false。
PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 expr 为 CoverParenthesizedExpressionAndArrowParameterList 所覆盖的 ParenthesizedExpression。
  2. 返回以 call 为参数对 expr 执行 HasCallInTailPosition 的结果。
ParenthesizedExpression : ( Expression )
  1. 返回以 call 为参数对 Expression 执行 HasCallInTailPosition 的结果。

15.10.3 PrepareForTailCall ( )

抽象操作 PrepareForTailCall,接收无参数;返回值:unused。 调用时执行以下步骤:

  1. 断言:当前执行上下文随后不会再用于对任何 ECMAScript 代码或内置函数进行求值。在调用此抽象操作之后调用 Call 时,会在执行任何此类求值之前创建并压入新的执行上下文。
  2. 丢弃与当前执行上下文关联的所有资源。
  3. 返回 unused。

尾位置调用必须在调用目标函数之前释放与当前正在执行的函数执行上下文关联的任何临时内部资源,或者复用这些资源来支持目标函数。

注

例如,尾位置调用使实现的活动记录栈增长的量,应仅为目标函数活动记录大小超出调用函数活动记录大小的部分。如果目标函数的活动记录更小,则栈的总大小应当减小。