Inheritance in JavaScript

Introduction

Inheritance lets a child class reuse and extend a parent class. JavaScript uses prototype chains; extends and super express parent/child relationships clearly. This chapter covers method overriding, calling parent constructors, and when composition is a better fit than deep inheritance trees.

Prerequisites

extends Base Class

super in Constructor

Child must call super() before using this:

Warning

Forgetting super() in a subclass constructor throws ReferenceError when you touch this.

super to Call Parent Methods

instanceof Along the Chain

javascript
const d = new Dog("Max");
console.log(d instanceof Dog);
console.log(d instanceof Animal);
console.log(d instanceof Object);

Overriding vs Overloading

JavaScript has no method overloading by signature—last definition wins. Use default parameters or different method names instead.

Prefer Composition When Possible

Many designs use small classes plus plain functions instead of five levels of extends.

Mini Example: Shape Hierarchy

FAQ

Can I extend built-ins like Array?

Possible but tricky—prefer composition or utility functions for extending array behavior.

Multiple inheritance?

Not directly—one extends parent; mixins or interfaces (TypeScript) address some cases.

Object.create vs extends?

extends is standard for class-based hierarchies; Object.create for one-off delegation.

What comes next?

Map and Set—built-in collections in the standard library.