In JavaScript ES6, if super()
must always be invoked in a subclass constructor before accessing this
, why isn't it automatically invoked by the JS engine?
Le me make an example:
class Animal {
constructor (name) {
this.name = name;
}
}
class Dog extends Animal {
constructor (name, breed) {
super();
this.breed = breed;
}
}
Couldn't it simply be?
class Dog extends Animal {
constructor (name, breed) {
this.breed = breed;
}
}
I mean, I know the second example doesn't work, but why hasn't this feature been built in JS to automate the process? Why do we have to type super()
each time? Is there a specific language design constraint that would otherwise break things?