JS Built-ins
Prototypesbeginner

Prototypes β€” super() calls parent constructor

πŸ“– Usage Example
class Animal {
  constructor(name) { this.name = name; }
}
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

Call super() first in the child constructor to initialize the parent's state.

Calling Parent Constructor with super()

When a subclass has its own constructor, you must call super() to call the parent's constructor. This initializes the parent's properties. Without super(), the parent properties won't be set up.

Challenge: Create a subclass constructor that calls the parent constructor using super().

πŸ’‘ Hints (3)
  • β€’ super() must be called before using this in subclass constructor
  • β€’ Arguments passed to super() go to parent constructor
  • β€’ Parent properties are initialized through super()