ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

15.4 Method Definitions

Syntax

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: Early Errors

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

15.4.2 Static Semantics: HasDirectSuper

The syntax-directed operation HasDirectSuper takes no arguments and returns a Boolean. It is defined piecewise over the following productions:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. If UniqueFormalParameters Contains SuperCall is true, return true.
  2. Return FunctionBody Contains SuperCall.
MethodDefinition : get ClassElementName ( ) { FunctionBody }
  1. Return FunctionBody Contains SuperCall.
MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }
  1. If PropertySetParameterList Contains SuperCall is true, return true.
  2. Return FunctionBody Contains SuperCall.
GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody }
  1. If UniqueFormalParameters Contains SuperCall is true, return true.
  2. Return GeneratorBody Contains SuperCall.
AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. If UniqueFormalParameters Contains SuperCall is true, return true.
  2. Return AsyncGeneratorBody Contains SuperCall.
AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. If UniqueFormalParameters Contains SuperCall is true, return true.
  2. Return AsyncFunctionBody Contains SuperCall.

15.4.3 Static Semantics: SpecialMethod

The syntax-directed operation SpecialMethod takes no arguments and returns a Boolean. It is defined piecewise over the following productions:

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

15.4.4 Runtime Semantics: DefineMethod

The syntax-directed operation DefineMethod takes argument object (an Object) and optional argument functionPrototype (an Object) and returns either a normal completion containing a Record with fields [[Key]] (a property key) and [[Closure]] (an ECMAScript function object) or an abrupt completion. It is defined piecewise over the following productions:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the running execution context's LexicalEnvironment.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. If functionPrototype is present, then
    1. Let prototype be functionPrototype.
  5. Else,
    1. Let prototype be %Function.prototype%.
  6. Let sourceText be the source text matched by MethodDefinition.
  7. Let closure be OrdinaryFunctionCreate(prototype, sourceText, UniqueFormalParameters, FunctionBody, non-lexical-this, env, privateEnv).
  8. Perform MakeMethod(closure, object).
  9. Return the Record { [[Key]]: propKey, [[Closure]]: closure }.

15.4.5 Runtime Semantics: MethodDefinitionEvaluation

The syntax-directed operation MethodDefinitionEvaluation takes arguments object (an Object) and enumerable (a Boolean) and returns either a normal completion containing either a PrivateElement or unused, or an abrupt completion. It is defined piecewise over the following productions:

MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
  1. Let methodDef be ? DefineMethod of MethodDefinition with argument object.
  2. Perform SetFunctionName(methodDef.[[Closure]], methodDef.[[Key]]).
  3. Return DefineMethodProperty(object, methodDef.[[Key]], methodDef.[[Closure]], enumerable).
MethodDefinition : get ClassElementName ( ) { FunctionBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the running execution context's LexicalEnvironment.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. Let sourceText be the source text matched by MethodDefinition.
  5. Let formalParameterList be an instance of the production FormalParameters : [empty] .
  6. Let closure be OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParameterList, FunctionBody, non-lexical-this, env, privateEnv).
  7. Perform MakeMethod(closure, object).
  8. Perform SetFunctionName(closure, propKey, "get").
  9. If propKey is a Private Name, then
    1. Return PrivateElement { [[Key]]: propKey, [[Kind]]: accessor, [[Get]]: closure, [[Set]]: undefined }.
  10. Else,
    1. Let desc be the PropertyDescriptor { [[Get]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }.
    2. Perform ? DefinePropertyOrThrow(object, propKey, desc).
    3. Return unused.
MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the running execution context's LexicalEnvironment.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. Let sourceText be the source text matched by MethodDefinition.
  5. Let closure be OrdinaryFunctionCreate(%Function.prototype%, sourceText, PropertySetParameterList, FunctionBody, non-lexical-this, env, privateEnv).
  6. Perform MakeMethod(closure, object).
  7. Perform SetFunctionName(closure, propKey, "set").
  8. If propKey is a Private Name, then
    1. Return PrivateElement { [[Key]]: propKey, [[Kind]]: accessor, [[Get]]: undefined, [[Set]]: closure }.
  9. Else,
    1. Let desc be the PropertyDescriptor { [[Set]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }.
    2. Perform ? DefinePropertyOrThrow(object, propKey, desc).
    3. Return unused.
GeneratorMethod : * ClassElementName ( UniqueFormalParameters ) { GeneratorBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the running execution context's LexicalEnvironment.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. Let sourceText be the source text matched by GeneratorMethod.
  5. Let closure be OrdinaryFunctionCreate(%GeneratorFunction.prototype%, sourceText, UniqueFormalParameters, GeneratorBody, non-lexical-this, env, privateEnv).
  6. Perform MakeMethod(closure, object).
  7. Perform SetFunctionName(closure, propKey).
  8. Let prototype be OrdinaryObjectCreate(%GeneratorFunction.prototype.prototype%).
  9. Perform ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  10. Return DefineMethodProperty(object, propKey, closure, enumerable).
AsyncGeneratorMethod : async * ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the running execution context's LexicalEnvironment.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. Let sourceText be the source text matched by AsyncGeneratorMethod.
  5. Let closure be OrdinaryFunctionCreate(%AsyncGeneratorFunction.prototype%, sourceText, UniqueFormalParameters, AsyncGeneratorBody, non-lexical-this, env, privateEnv).
  6. Perform MakeMethod(closure, object).
  7. Perform SetFunctionName(closure, propKey).
  8. Let prototype be OrdinaryObjectCreate(%AsyncGeneratorFunction.prototype.prototype%).
  9. Perform ! DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  10. Return DefineMethodProperty(object, propKey, closure, enumerable).
AsyncMethod : async ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. Let propKey be ? Evaluation of ClassElementName.
  2. Let env be the LexicalEnvironment of the running execution context.
  3. Let privateEnv be the running execution context's PrivateEnvironment.
  4. Let sourceText be the source text matched by AsyncMethod.
  5. Let closure be OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, UniqueFormalParameters, AsyncFunctionBody, non-lexical-this, env, privateEnv).
  6. Perform MakeMethod(closure, object).
  7. Perform SetFunctionName(closure, propKey).
  8. Return DefineMethodProperty(object, propKey, closure, enumerable).