15.7 Class Definitions
Syntax
A class definition is always
15.7.1 Static Semantics: Early Errors
-
It is a Syntax Error if
ClassHeritage is not present and the following algorithm evaluates totrue :- Let constructor be
ConstructorMethod ofClassBody . - If constructor is
empty , returnfalse . - Return
HasDirectSuper of constructor.
- Let constructor be
-
It is a Syntax Error if
PrototypePropertyNameList ofClassElementList contains more than one occurrence of"constructor" .
-
It is a Syntax Error if
PropName ofMethodDefinition is not"constructor" andHasDirectSuper ofMethodDefinition istrue . -
It is a Syntax Error if
PropName ofMethodDefinition is"constructor" andSpecialMethod ofMethodDefinition istrue .
-
It is a Syntax Error if
HasDirectSuper ofMethodDefinition istrue . -
It is a Syntax Error if
PropName ofMethodDefinition is"prototype" .
15.7.2 Static Semantics: ClassElementKind
- If
PropName ofMethodDefinition is"constructor" , returnConstructorMethod . - Return
NonConstructorMethod .
- Return
NonConstructorMethod .
- Return
empty .
15.7.3 Static Semantics: ConstructorMethod
- If
ClassElementKind ofClassElement isConstructorMethod , returnClassElement . - Return
empty .
- Let head be
ConstructorMethod ofClassElementList . - If head is not
empty , return head. - If
ClassElementKind ofClassElement isConstructorMethod , returnClassElement . - Return
empty .
Early Error rules ensure that there is only one method definition named
15.7.4 Static Semantics: IsStatic
- Return
false .
- Return
true .
- Return
false .
15.7.5 Static Semantics: NonConstructorMethodDefinitions
- If
ClassElementKind ofClassElement isNonConstructorMethod , then- Return a
List whose sole element isClassElement .
- Return a
- Return a new empty
List .
- Let list be
NonConstructorMethodDefinitions ofClassElementList . - If
ClassElementKind ofClassElement isNonConstructorMethod , then- Append
ClassElement to the end of list.
- Append
- Return list.
15.7.6 Static Semantics: PrototypePropertyNameList
- If
PropName ofClassElement isempty , return a new emptyList . - If
IsStatic ofClassElement istrue , return a new emptyList . - Return a
List whose sole element isPropName ofClassElement .
- Let list be
PrototypePropertyNameList ofClassElementList . - If
PropName ofClassElement isempty , return list. - If
IsStatic ofClassElement istrue , return list. - Append
PropName ofClassElement to the end of list. - Return list.
15.7.7 Runtime Semantics: ClassDefinitionEvaluation
With parameters classBinding and className.
- Let env be the LexicalEnvironment of the
running execution context . - Let classScope be
NewDeclarativeEnvironment (env). - If classBinding is not
undefined , then- Perform classScope.CreateImmutableBinding(classBinding,
true ).
- Perform classScope.CreateImmutableBinding(classBinding,
- If
ClassHeritage is not present, thenopt - Let protoParent be
%Object.prototype% . - Let constructorParent be
%Function.prototype% .
- Let protoParent be
- Else,
- Set the
running execution context 's LexicalEnvironment to classScope. - Let superclassRef be the result of evaluating
ClassHeritage . - Set the
running execution context 's LexicalEnvironment to env. - Let superclass be ?
GetValue (superclassRef). - If superclass is
null , then- Let protoParent be
null . - Let constructorParent be
%Function.prototype% .
- Let protoParent be
- Else if
IsConstructor (superclass) isfalse , throw aTypeError exception. - Else,
- Set the
- Let proto be !
OrdinaryObjectCreate (protoParent). - If
ClassBody is not present, let constructor beopt empty . - Else, let constructor be
ConstructorMethod ofClassBody . - Set the
running execution context 's LexicalEnvironment to classScope. - If constructor is
empty , then- Let steps be the algorithm steps defined in
Default Constructor Functions . - Let F be !
CreateBuiltinFunction (steps, 0, className, « [[ConstructorKind]], [[SourceText]] »,empty , constructorParent).
- Let steps be the algorithm steps defined in
- Else,
- Let constructorInfo be !
DefineMethod of constructor with arguments proto and constructorParent. - Let F be constructorInfo.[[Closure]].
- Perform !
MakeClassConstructor (F). - Perform !
SetFunctionName (F, className).
- Let constructorInfo be !
- Perform !
MakeConstructor (F,false , proto). - If
ClassHeritage is present, set F.[[ConstructorKind]] toopt derived . - Perform !
CreateMethodProperty (proto,"constructor" , F). - If
ClassBody is not present, let methods be a new emptyopt List . - Else, let methods be
NonConstructorMethodDefinitions ofClassBody . - For each
ClassElement m of methods, do- If
IsStatic of m isfalse , then- Let status be
PropertyDefinitionEvaluation of m with arguments proto andfalse .
- Let status be
- Else,
- Let status be
PropertyDefinitionEvaluation of m with arguments F andfalse .
- Let status be
- If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to env. - Return
Completion (status).
- Set the
- If
- Set the
running execution context 's LexicalEnvironment to env. - If classBinding is not
undefined , then- Perform classScope.InitializeBinding(classBinding, F).
- Return F.
15.7.7.1 Default Constructor Functions
When a Default
- If NewTarget is
undefined , throw aTypeError exception. - Let F be the
active function object . - If F.[[ConstructorKind]] is
derived , then- 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 DefaultConstructor Function does not. - Let func be ! F.[[GetPrototypeOf]]().
- If
IsConstructor (func) isfalse , throw aTypeError exception. - Return ?
Construct (func, args, NewTarget).
- NOTE: This branch behaves similarly to
- Else,
- NOTE: This branch behaves similarly to
constructor() {}
. - Return ?
OrdinaryCreateFromConstructor (NewTarget,"%Object.prototype%" ).
- NOTE: This branch behaves similarly to
The
15.7.8 Runtime Semantics: BindingClassDeclarationEvaluation
- Let className be
StringValue ofBindingIdentifier . - Let value be ?
ClassDefinitionEvaluation ofClassTail with arguments className and className. - Set value.[[SourceText]] to the source text matched by
ClassDeclaration . - Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName (className, value, env). - Return value.
- Let value be ?
ClassDefinitionEvaluation ofClassTail with argumentsundefined and"default" . - Set value.[[SourceText]] to the source text matched by
ClassDeclaration . - Return value.
15.7.9 Runtime Semantics: Evaluation
- Perform ?
BindingClassDeclarationEvaluation of thisClassDeclaration . - Return
NormalCompletion (empty ).
- Let value be ?
ClassDefinitionEvaluation ofClassTail with argumentsundefined and"" . - Set value.[[SourceText]] to the source text matched by
ClassExpression . - Return value.
- Let className be
StringValue ofBindingIdentifier . - Let value be ?
ClassDefinitionEvaluation ofClassTail with arguments className and className. - Set value.[[SourceText]] to the source text matched by
ClassExpression . - Return value.