devnotes.

Classes, Constructors, and Inheritance

Classes (ES2015) provide a cleaner syntax for JavaScript's existing prototype-based object model.

class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a sound.`;
  }
}

class Dog extends Animal {
  speak() {
    return `${this.name} barks.`;
  }
}

new Dog("Rex").speak(); // "Rex barks."

The constructor method runs automatically when you create an instance with new, and is where you typically assign initial properties to this. extends sets up the prototype chain so Dog inherits everything from Animal, and speak() here overrides the parent's version — this is polymorphism in practice.

Inside a subclass's constructor, super(...) must be called before using this, since it runs the parent class's constructor first — skipping it throws a ReferenceError. A subclass method can also call super.speak() to run the parent's version and build on top of it rather than fully replacing it.

JavaScript Classes, Constructors, and Inheritance | DevNotes