🔰inheritance
JS ⟩ value ⟩ object ⟩ class ⟩ inheritance
access properties on object literal / class's prototype object. (
super.prop/super[expr]are valid in any method definition in object literal / class)invoke superclass constructor. (
super(...args)is valid in class constructors)
// in any method definition
super.prop // parent's property
super[expr] // (same)
// ⭐️ subclass
class B extends A {
// ⭐️ subclass constructor
// (if omitted, a default constructor is created automatically)
constructor(...args) {
// ⭐️ must call super() before referencing `this`
super(...args) // ⭐️ superclass constructor
// intialize `this` ...
}
// override method
method() {
super.method() // ⭐️ call parent's method (optional)
// ...
}
}super - invoke/access superclass/parent's constructor/method/properties.
if a class extends another class and has no constructor(), then the following “default” constructor() is generated:
class B extends A {
constructor(...args) { super(...args) } // ⭐️ auto-generated
}differences between class A and class A extends Object
class A class A extends Object
---------------------------------------------------------------
must call super() No Yes
in constructor
---------------------------------------------------------------
A.__proto__ Function.prototype Object
---------------------------------------------------------------👉 See: JS.info ⟩ Class extends Object?
use prototype.
Last updated
Was this helpful?