Class Inheritance
suggest changeInheritance works just like it does in other object-oriented languages: methods defined on the superclass are accessible in the extending subclass.
If a derived class declares its own constructor, it must invoke the parent constructor with super() before accessing this.
class SuperClass {
constructor() {
this.logger = console.log;
}
log() {
this.logger(`Hello ${this.name}`);
}
}
class SubClass extends SuperClass {
constructor() {
super();
this.name = 'subclass';
}
}
const subClass = new SubClass();
subClass.log(); // logs: "Hello subclass"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents