extends

Creates a derived class that inherits another class’s methods and can extend or override them.

On this page

Example

#
JavaScript
class Exercise {
  constructor(title, points) {
    this.title = title
    this.points = points
  }

  describe() {
    return `${this.title} (${this.points} pts)`
  }
}

class Quiz extends Exercise {
  constructor(title, points, questions) {
    super(title, points)
    this.questions = questions
  }
}

const quiz = new Quiz('Loops quiz', 10, 5)

console.log(quiz.describe())
console.log(quiz)
console.log(quiz instanceof Exercise)
Console
Loops quiz (10 pts)
Quiz { title: 'Loops quiz', points: 10, questions: 5 }
true

Definition and usage

#

The extends keyword in a class declaration names the base class, the parent. The derived class gets all of its methods and can add its own, or override an inherited one by writing a method with the same name. Underneath, the prototype chain does the work: the derived class’s prototype points to the base class’s prototype.

If the derived class has its own constructor, it must call super(...) before using this for the first time. It’s the parent constructor that creates the object, and only afterward can the derived class add its own fields. When a derived class has no constructor, JavaScript adds a default one that forwards all the arguments to the parent.

After extends can come any expression that returns a constructor, including a built-in class like Error, Array, or Map. You can only inherit from one class. When you want to combine behavior from several sources, composition, keeping other objects in fields, is usually the better choice.

Syntax

#
Syntax
class Child extends Parent {
  constructor(...args) {
    super(...args)
  }
}

class MyError extends Error {}

Parameters

#
  • Parent

    class

    The base class the new class inherits from. It can also be a built-in class, like Error or Array.

More examples

#
Overriding a method, and the default constructor
JavaScript
class Exercise {
  constructor(title) {
    this.title = title
  }

  reward() {
    return 5
  }
}

class Project extends Exercise {
  reward() {
    return 25
  }
}

const tasks = [new Exercise('Multiple-choice question'), new Project('Portfolio website')]

for (const task of tasks) {
  console.log(task.title, task.reward())
}
Console
Multiple-choice question 5
Portfolio website 25

The Project class has no constructor, so JavaScript used the default one, which forwarded the title to the Exercise constructor.

Pitfall: this before super()
JavaScript
class Student {
  constructor(name) {
    this.name = name
  }
}

class Mentor extends Student {
  constructor(name, course) {
    this.course = course
    super(name)
  }
}

try {
  new Mentor('Jack', 'React')
} catch (error) {
  console.log(error.name)
}
Console
ReferenceError

The object is only created in the parent constructor, so before super() runs there’s nothing yet to assign to this. The exact error message differs between browsers.

Extending a built-in class
JavaScript
class Playlist extends Array {
  totalMinutes() {
    return this.reduce((sum, video) => sum + video.minutes, 0)
  }
}

const playlist = new Playlist()
playlist.push({ title: 'Intro', minutes: 4 }, { title: 'Loops', minutes: 11 })

console.log(playlist.length)
console.log(playlist.totalMinutes())
console.log(Array.isArray(playlist))
Console
2
15
true

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Only inherit when the derived class really is a variant of the base class, for example a quiz is an exercise. When an object merely has another object, for example a player has inventory, choose composition instead.
  • In a derived class’s constructor, call super() right at the start. Using this before it ends in a ReferenceError.
  • Avoid deep hierarchies. Two or three levels of inheritance is usually a sensible maximum; beyond that, the code gets hard to follow.

Related entries

#

Spotted a mistake or missing an example? Write to us.