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...
- I need to access
this
from within the constructor - The superclass constructor requires different arguments then the derived class constructor
Are there other times when I should include a call to the superclass constructor?
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...
- I need to access
this
from within the constructor - The superclass constructor requires different arguments then the derived class constructor
Are there other times when I should include a call to the superclass constructor?
Share Improve this question edited Dec 27, 2016 at 19:18 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Dec 27, 2016 at 19:16 sfletchesfletche 49.7k31 gold badges108 silver badges120 bronze badges 5 |2 Answers
Reset to default 11Yes, that sounds correct, albeit a bit oddly formulated. The rules should be
- In a derived class, you always1 need to call the
super(…)
constructor - If you are not doing more than the default constructor, you can omit the whole
constructor(){}
, 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.
You need to call super
in a subclass constructor in these cases:
- You want to reference
this
in the subclass constructor - You don't return a different object in the subclass constructor
In 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.
class ABC extends DEF { ...
then you have to putsuper(...args);
, because you just stated withextends DEF
that you want to derive from it. If you don't derive from any class then just don't put 'super' in the ABC's constructor. It's that simple. – Azamantes Commented Dec 27, 2016 at 19:23super
if the parent class takes the same arguments as the child class. – Felix Kling Commented Dec 27, 2016 at 19:24super
call" for all constructors, it's only part of the default constructor. – Bergi Commented Dec 27, 2016 at 19:29