override constructor
✅ case 1:none has constructor
// no constructors
class A {}
class B extends A {}
let b = new B(); // ✅ OK❌ case 2:returning from derived constructor without calling super() first
class A2 {}
class B2 extends A2 {
constructor(){
// ---------------------------------------------------
// ⛔ ReferenceError:
// Must call `super()` constructor in derived class
// ☐ before accessing `this` or
// ☑ returning from derived constructor
// ---------------------------------------------------
}
}
let b2 = new B2();❌ case 3:accessing this before calling super()
✅ case 4:
🔸 default derived constructor
if a class extends another class and has no constructor(), then the following “default” constructor() is generated:
🔸 derived constructor
super is used in derived constructor or overridden methods.
Last updated
Was this helpful?