Example 1 — super.method()
Reuse parent behavior.
JavaScript
class A { hi(){ return "A"; } }
class B extends A { hi(){ return super.hi() + "B"; } }
console.log(new B().hi()); How It Works
- super.hi() calls A.
class Child extends Parent sets up prototype inheritance. Use super in the constructor and super.method() to call parent methods.
class Animal {
speak() { return "…"; }
}
class Dog extends Animal {
speak() { return "woof"; }
}
console.log(new Dog().speak());
console.log(new Dog() instanceof Animal); Reuse parent behavior.
class A { hi(){ return "A"; } }
class B extends A { hi(){ return super.hi() + "B"; } }
console.log(new B().hi()); Child.prototype → Parent.prototype.
class A {}
class B extends A {}
console.log(Object.getPrototypeOf(B.prototype) === A.prototype); Possible for Error, Array in modern engines.
class E extends Error {}
console.log(new E("x") instanceof Error); CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful