Description
The super
keyword can be used in two ways: as a "function call" (super(...args)
), or as a "property lookup" (super.prop
and super[expr]
).
Note: super
is a keyword and these are special syntactic constructs. super
is not a variable that points to the prototype object. Attempting to read super
itself is a SyntaxError
.
const child = { myParent() { console.log(super); // SyntaxError: 'super' keyword unexpected here }, };
In the constructor body of a derived class (with extends
), the super
keyword may appear as a "function call" (super(...args)
), which must be called before the this
keyword is used, and before the constructor returns. It calls the
parent class's constructor and binds the parent class's public fields,
after which the derived class's constructor can further access and
modify this
.
The "property lookup" form can be used to access methods and
properties of an object literal's or class's [[Prototype]]. Within a
class's body, the reference of super
can be either the superclass's constructor itself, or the constructor's prototype
,
depending on whether the execution context is instance creation or
class initialization. See the Examples section for more details.
Note that the reference of super
is determined by the class or object literal super
was declared in, not the object the method is called on. Therefore,
unbinding or re-binding a method doesn't change the reference of super
in it (although they do change the reference of this
). You can see super
as a variable in the class or object literal scope, which the methods
create a closure over. (But also beware that it's not actually not a
variable, as explained above).
When setting properties through super
, the property is set on this
instead.