Course contentsShow
← JavaScript
Lesson 770 of 2,72318. ClassesFree lesson

The super() Constructor Call

Calling super() in the child constructor to invoke the parent constructor.

The super() Constructor Call

In this lesson, you'll learn how to properly initialize a parent class from within a child class constructor.

When you create a child class using extends, you need a way to call the parent class's constructor to set up inherited properties. This is where super() comes in — it's a special function call that invokes the parent constructor.

Why super() Is Required

Think of super() as asking your parent to finish their part of the setup before you add your own custom touches. When a child class has its own constructor, JavaScript requires you to call super() before you can use this or return from the constructor. This ensures the parent class properly initializes the object first.

How to Use super()

You call super() just like a function, passing any arguments the parent constructor expects:

class Animal {
  constructor(name) {
    this.name = name;
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name); // Calls Animal constructor with 'name'
    this.breed = breed; // Now we can add Dog-specific properties
  }
}

const myDog = new Dog('Buddy', 'Golden Retriever');
console.log(myDog.name);  // 'Buddy' (set by parent)
console.log(myDog.breed); // 'Golden Retriever' (set by child)

The Critical Rule

You must call super() before accessing this in a child constructor. Trying to use this before super() will throw an error because the object isn't fully created yet.

Key Takeaway: The super() call invokes the parent class constructor, and it must be called before using this in any child class constructor.