Hiprup

What is the difference between class inheritance and prototypal inheritance?

Two views of the same underlying mechanism.

  • Class inheritance — the class/extends syntax: familiar and clean, but sugar over prototypes.

  • Prototypal inheritance — objects link directly to other objects via the prototype chain (Object.create).

Key point: JavaScript only truly has prototypal inheritance — classes are a friendlier syntax on top.

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

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

// Prototypal (original)
const animal = {
  speak() { return `${this.name} makes a sound`; }
};

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

// Both work the same way
console.log(new Dog('Buddy').speak()); // 'Buddy barks'
console.log(dog.speak());               // 'Rex barks'

// Proof: classes ARE prototypal
console.log(typeof Animal); // 'function'
console.log(Dog.prototype.__proto__ === Animal.prototype); // true

Class syntax provides extends/super/constructor. Object.create directly links prototypes.

Both create the same prototype chain. typeof Animal is 'function' (not 'class'). Dog.prototype.proto === Animal.prototype proves classes use prototypes internally.

Classes are syntactic sugar over prototypes — prove it (typeof MyClass = 'function', prototype chain identical). Both approaches create the same result. Classes are preferred for readability.

Object.create is lower-level but more flexible. Know that JS does not have 'real' class-based inheritance like Java.

What is the difference between class inheritance and prototypal inheritance? | Hiprup