Class Inheritance

suggest change

Inheritance 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"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents