Reputation: 49714
Reading Dr. Axel Rauschmayer's blog on ES6 classes, I understand that a derived class has the following default constructor when none is provided
constructor(...args) {
super(...args);
}
I also understand that if I want to use this
within a constructor I first need to call super
, otherwise this
will not yet be initialized (throwing a ReferenceError).
constructor(width, height) {
this.width = width; // ReferenceError
super(width, height);
this.height = height; // no error thrown
...
}
Is the following assumption then correct? (and if not, could you please explain the conditions under which I should explicitly call super
)
For derived classes, I only need to explicitly call super
when...
this
from within the constructorAre there other times when I should include a call to the superclass constructor?
Upvotes: 9
Views: 14324
Reputation: 288150
You need to call super
in a subclass constructor in these cases:
this
in the subclass constructorIn other cases, you can call it if you want the superclass constructor to run, but you don't have to.
class SuperClass{
constructor() {
console.log('SuperClass');
}
}
class SubClass1 extends SuperClass {
constructor() {
console.log('SubClass1');
super();
return {};
}
}
class SubClass2 extends SuperClass {
constructor() {
console.log('SubClass2');
return {};
}
}
new SubClass1();
new SubClass2();
I don't see how the order of arguments matters when deciding whether you should call super
or not.
Upvotes: 4
Reputation: 664548
Yes, that sounds correct, albeit a bit oddly formulated. The rules should be
super(…)
constructorconstructor(){}
,
which in turn will make your class code not contain a super call.1: You don't need to call it in the suspicious edge case of explicitly return
ing an object, which you hardly ever would.
Upvotes: 11