override methods

💾 程式:replit

// superclass
class A {
    method() { log(`a.method() runs.`) }    // ⭐ super.method()
}

// subclass
class B extends A {
    
    // ⭐ replace `super.method()`
    method() { log(`b.method() runs.`) }     // "b.method() runs."

    // ⭐ extend `super.method()`
    method() {
        log(`b.method() runs first, then:`)  // "b.method() runs first, then"
        super.method();                      // "a.method() runs."
    }
}

const bunny = new B();
bunny.method();   

Last updated