Trivia Questions2 min readPremium

Classical vs Prototypal Inheritance in JavaScript

What is the difference between classical inheritance and prototypal inheritance?

Inheritance is a fundamental concept in object-oriented programming that allows one object to inherit properties and methods from another. In JavaScript, two primary types of inheritance are commonly discussed: classical inheritance and prototypal inheritance. Although JavaScript is inherently a prototype-based language, it can simulate classical inheritance patterns. Understanding the distinction is essential for building efficient and maintainable applications. MDN’s guide to inheritance and the prototype chain goes deeper.

1. Classical Inheritance

Classical inheritance is the model used in many object-oriented languages like Java, C++, and C#. In this model, classes are blueprints for creating objects. Subclasses inherit from parent classes using keywords like extends or super.

Example (Classical Style in ES6):

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

  speak() {
    return `${this.name} makes a noise.`;
  }
}

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

const dog = new Dog("Rex");
console.log(dog.speak()); // Rex barks.

Key Characteristics:

  • Class-based syntax
  • Constructor functions define object structure
  • Methods are defined on the class prototype
  • Inheritance achieved through subclassing

2. Prototypal Inheritance

Prototypal inheritance uses objects directly. An object can inherit from another object without the need for classes. JavaScript natively supports this model using Object.create, object literals, and prototypes.

Example (Prototypal Style):

const animal = {
  speak() {
    return `${this.name} makes a noise.`;
  }
};

const dog = Object.create(animal);
dog.name = "Rex";
dog.speak = function() {
  return `${this.name} barks.`;
};

console.log(dog.speak()); // Rex barks.

Key Characteristics:

  • Object-based
  • Inheritance via prototype chain
  • More flexible and compositional
  • No need for constructors or classes

3. Under the Hood: How JavaScript Uses Prototypes

Every object in JavaScript has an internal link to another object called its prototype. When accessing a property, JavaScript checks the object. If it doesn’t exist, it walks up the prototype chain.

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  return `${this.name} makes a noise.`;
};

const dog = new Animal("Rex");
console.log(dog.speak()); // Rex makes a noise.

Here, speak is attached to Animal.prototype, which dog inherits from via the prototype chain.

4. Classical vs Prototypal Inheritance: Key Differences

Aspect Classical Inheritance Prototypal Inheritance
Structure Class-based Object-based
Syntax Uses class, extends, super Uses Object.create or literal objects
Inheritance Subclassing Prototype chaining
Flexibility More rigid More flexible and composable
Performance Comparable in most modern engines Comparable in most modern engines

5. When to Use Which

If you’re working in modern JavaScript with frameworks or tools that expect class-based syntax (like React), classical inheritance might feel more familiar and readable.

For highly dynamic use cases or when you want to compose behaviors across unrelated objects, prototypal inheritance may offer more flexibility.

Conclusion

While JavaScript now supports classical inheritance through ES6 class syntax, under the hood it’s still using prototypal inheritance. Understanding both styles helps you write better, more idiomatic JavaScript. Choose the model that best fits your use case and codebase architecture.