Pattern: Use of this
before super()
call
Issue: -
In derived class constructors, super()
must be called before accessing this
or super
. Accessing this
before super()
is called will throw a reference error as the object is not yet initialized.
Example of incorrect code:
class Animal extends Living {
constructor() {
this.legs = 4; // ReferenceError
super();
}
}
class Dog extends Animal {
constructor() {
this.bark(); // ReferenceError
super();
this.legs = 4;
}
}
Example of correct code:
class Animal extends Living {
constructor() {
super();
this.legs = 4; // Valid after super()
}
}
class Dog extends Animal {
constructor() {
super();
this.bark(); // Valid after super()
this.legs = 4;
}
}