15.7 Class Definitions

Syntax

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] ; Note

A class definition is always strict mode code.

15.7.1 Static Semantics: Early Errors

ClassTail : ClassHeritageopt { ClassBody } ClassBody : ClassElementList ClassElement : MethodDefinition ClassElement : static MethodDefinition

15.7.2 Static Semantics: ClassElementKind

ClassElement : MethodDefinition
  1. If PropName of MethodDefinition is "constructor", return ConstructorMethod.
  2. Return NonConstructorMethod.
ClassElement : static MethodDefinition
  1. Return NonConstructorMethod.
ClassElement : ;
  1. Return empty.

15.7.3 Static Semantics: ConstructorMethod

ClassElementList : ClassElement
  1. If ClassElementKind of ClassElement is ConstructorMethod, return ClassElement.
  2. Return empty.
ClassElementList : ClassElementList ClassElement
  1. Let head be ConstructorMethod of ClassElementList.
  2. If head is not empty, return head.
  3. If ClassElementKind of ClassElement is ConstructorMethod, return ClassElement.
  4. Return empty.
Note

Early Error rules ensure that there is only one method definition named "constructor" and that it is not an accessor property or generator definition.

15.7.4 Static Semantics: IsStatic

ClassElement : MethodDefinition
  1. Return false.
ClassElement : static MethodDefinition
  1. Return true.
ClassElement : ;
  1. Return false.

15.7.5 Static Semantics: NonConstructorMethodDefinitions

ClassElementList : ClassElement
  1. If ClassElementKind of ClassElement is NonConstructorMethod, then
    1. Return a List whose sole element is ClassElement.
  2. Return a new empty List.
ClassElementList : ClassElementList ClassElement
  1. Let list be NonConstructorMethodDefinitions of ClassElementList.
  2. If ClassElementKind of ClassElement is NonConstructorMethod, then
    1. Append ClassElement to the end of list.
  3. Return list.

15.7.6 Static Semantics: PrototypePropertyNameList

ClassElementList : ClassElement
  1. If PropName of ClassElement is empty, return a new empty List.
  2. If IsStatic of ClassElement is true, return a new empty List.
  3. Return a List whose sole element is PropName of ClassElement.
ClassElementList : ClassElementList ClassElement
  1. Let list be PrototypePropertyNameList of ClassElementList.
  2. If PropName of ClassElement is empty, return list.
  3. If IsStatic of ClassElement is true, return list.
  4. Append PropName of ClassElement to the end of list.
  5. Return list.

15.7.7 Runtime Semantics: ClassDefinitionEvaluation

With parameters classBinding and className.

ClassTail : ClassHeritageopt { ClassBodyopt }
  1. Let env be the LexicalEnvironment of the running execution context.
  2. Let classScope be NewDeclarativeEnvironment(env).
  3. If classBinding is not undefined, then
    1. Perform classScope.CreateImmutableBinding(classBinding, true).
  4. If ClassHeritageopt is not present, then
    1. Let protoParent be %Object.prototype%.
    2. Let constructorParent be %Function.prototype%.
  5. Else,
    1. Set the running execution context's LexicalEnvironment to classScope.
    2. Let superclassRef be the result of evaluating ClassHeritage.
    3. Set the running execution context's LexicalEnvironment to env.
    4. Let superclass be ? GetValue(superclassRef).
    5. If superclass is null, then
      1. Let protoParent be null.
      2. Let constructorParent be %Function.prototype%.
    6. Else if IsConstructor(superclass) is false, throw a TypeError exception.
    7. Else,
      1. Let protoParent be ? Get(superclass, "prototype").
      2. If Type(protoParent) is neither Object nor Null, throw a TypeError exception.
      3. Let constructorParent be superclass.
  6. Let proto be ! OrdinaryObjectCreate(protoParent).
  7. If ClassBodyopt is not present, let constructor be empty.
  8. Else, let constructor be ConstructorMethod of ClassBody.
  9. Set the running execution context's LexicalEnvironment to classScope.
  10. If constructor is empty, then
    1. Let steps be the algorithm steps defined in Default Constructor Functions.
    2. Let F be ! CreateBuiltinFunction(steps, 0, className, « [[ConstructorKind]], [[SourceText]] », empty, constructorParent).
  11. Else,
    1. Let constructorInfo be ! DefineMethod of constructor with arguments proto and constructorParent.
    2. Let F be constructorInfo.[[Closure]].
    3. Perform ! MakeClassConstructor(F).
    4. Perform ! SetFunctionName(F, className).
  12. Perform ! MakeConstructor(F, false, proto).
  13. If ClassHeritageopt is present, set F.[[ConstructorKind]] to derived.
  14. Perform ! CreateMethodProperty(proto, "constructor", F).
  15. If ClassBodyopt is not present, let methods be a new empty List.
  16. Else, let methods be NonConstructorMethodDefinitions of ClassBody.
  17. For each ClassElement m of methods, do
    1. If IsStatic of m is false, then
      1. Let status be PropertyDefinitionEvaluation of m with arguments proto and false.
    2. Else,
      1. Let status be PropertyDefinitionEvaluation of m with arguments F and false.
    3. If status is an abrupt completion, then
      1. Set the running execution context's LexicalEnvironment to env.
      2. Return Completion(status).
  18. Set the running execution context's LexicalEnvironment to env.
  19. If classBinding is not undefined, then
    1. Perform classScope.InitializeBinding(classBinding, F).
  20. Return F.

15.7.7.1 Default Constructor Functions

When a Default Constructor Function is called with zero or more arguments which form the rest parameter ...args, the following steps are taken:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Let F be the active function object.
  3. If F.[[ConstructorKind]] is derived, then
    1. NOTE: This branch behaves similarly to constructor(...args) { super(...args); }. The most notable distinction is that while the aforementioned ECMAScript source text observably calls the @@iterator method on %Array.prototype%, a Default Constructor Function does not.
    2. Let func be ! F.[[GetPrototypeOf]]().
    3. If IsConstructor(func) is false, throw a TypeError exception.
    4. Return ? Construct(func, args, NewTarget).
  4. Else,
    1. NOTE: This branch behaves similarly to constructor() {}.
    2. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Object.prototype%").

The "length" property of a default constructor function is +0𝔽.

15.7.8 Runtime Semantics: BindingClassDeclarationEvaluation

ClassDeclaration : class BindingIdentifier ClassTail
  1. Let className be StringValue of BindingIdentifier.
  2. Let value be ? ClassDefinitionEvaluation of ClassTail with arguments className and className.
  3. Set value.[[SourceText]] to the source text matched by ClassDeclaration.
  4. Let env be the running execution context's LexicalEnvironment.
  5. Perform ? InitializeBoundName(className, value, env).
  6. Return value.
ClassDeclaration : class ClassTail
  1. Let value be ? ClassDefinitionEvaluation of ClassTail with arguments undefined and "default".
  2. Set value.[[SourceText]] to the source text matched by ClassDeclaration.
  3. Return value.
Note

ClassDeclaration : class ClassTail only occurs as part of an ExportDeclaration and establishing its binding is handled as part of the evaluation action for that production. See 16.2.3.7.

15.7.9 Runtime Semantics: Evaluation

ClassDeclaration : class BindingIdentifier ClassTail
  1. Perform ? BindingClassDeclarationEvaluation of this ClassDeclaration.
  2. Return NormalCompletion(empty).
Note

ClassDeclaration : class ClassTail only occurs as part of an ExportDeclaration and is never directly evaluated.

ClassExpression : class ClassTail
  1. Let value be ? ClassDefinitionEvaluation of ClassTail with arguments undefined and "".
  2. Set value.[[SourceText]] to the source text matched by ClassExpression.
  3. Return value.
ClassExpression : class BindingIdentifier ClassTail
  1. Let className be StringValue of BindingIdentifier.
  2. Let value be ? ClassDefinitionEvaluation of ClassTail with arguments className and className.
  3. Set value.[[SourceText]] to the source text matched by ClassExpression.
  4. Return value.