super

super keyword is used within a subclass to call the constructor or methods of its parent class. It's a way to access and invoke functionalities defined in the parent class while extending or customizing them in the child class. super helps maintain inheritance and allows you to reuse code from the parent class in the child class, promoting code reusability in object-oriented programming.


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

class Rabbit extends Animal {
  constructor(name, age, runSpeed) {
    super(name, age); // refers to the parent class Animal
    this.runSpeed = runSpeed;
  }
}
class Fish extends Animal {
  constructor(name, age, swimSpeed) {
    super(name, age); // refers to the parent class Animal
    this.swimSpeed = swimSpeed;
  }
}
class Bird extends Animal {
  constructor(name, age, flySpeed) {
    super(name, age); // refers to the parent class Animal
    this.flySpeed = flySpeed;
  }
}

//
const bird = new Bird("Steve", 2, 120);
console.log(bird.name);
console.log(bird.age);
console.log(bird.flySpeed);