JavaScript Class extends, super and Inheritance
Quick answer
extends links a derived class to a base class and establishes prototype inheritance for instance methods and constructor inheritance for static members.
How it works
class Child extends Parent connects Child.prototype to Parent.prototype and Child to Parent. A derived constructor ordinarily invokes super(...) to initialize its receiver. In an overriding method, super.method() looks up the method relative to the method’s home object while keeping the current this receiver. The base method can therefore observe derived instance state.
class Base { constructor(n) { this.n=n; } label() { return "B"+this.n; } }class Child extends Base { label() { return super.label()+"!"; } }const x = new Child(4); console.log(x.label(),x instanceof Base);Expected output in Node.js:
B4! trueThe inherited constructor initializes n and super.label uses the child receiver.
class Base { static code() { return 5; } } class Child extends Base {}console.log(Child.code(),Object.getPrototypeOf(Child)===Base);Expected output in Node.js:
5 trueThe constructor itself also inherits from the base constructor.
Common mistakes
Do not interpret super as a separate parent instance. It provides special lookup or constructor invocation behavior.
Practice
Trace both constructor and instance prototype chains for Child.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Class extends, super and Inheritance — Knowledge Check
Apply the taught concept and predict the output.
What is the ordinary prototype of Child.prototype in class Child extends Base?
What receiver does super.method() preserve?
How can Child inherit public static methods from Base?
What is the exact console output in JavaScript Class extends, super and Inheritance example 1?
class Base { constructor(n) { this.n=n; } label() { return "B"+this.n; } }class Child extends Base { label() { return super.label()+"!"; } }const x = new Child(4); console.log(x.label(),x instanceof Base);What is the exact console output in JavaScript Class extends, super and Inheritance example 2?
class Base { static code() { return 5; } } class Child extends Base {}console.log(Child.code(),Object.getPrototypeOf(Child)===Base);