In classical inheritance (Java, C#, C++), classes are blueprints and objects are instances created from them. The class hierarchy is fixed at compile time and instance structure is copied from the class definition. In prototypal inheritance (JavaScript), objects inherit directly from other objects: each object has a live [[Prototype]] link, and property lookups delegate up that chain at runtime. JavaScript's class syntax is a classical-looking layer over prototypes, not a different model.
Dog extends Animal), established when the code is compiled.const animal = {
speak() { return `${this.name} makes a sound`; },
};
const dog = Object.create(animal); // dog's prototype IS the animal object
dog.name = 'Rex';
dog.speak(); // 'Rex makes a sound', found on animal via delegation
animal.speak = function () { return `${this.name} speaks!`; };
dog.speak(); // 'Rex speaks!', the link is live, not a copy
Object.getPrototypeOf(dog) === animal; // true
Lookups walk dog → animal → Object.prototype → null. Assigning dog.speak = … creates an own property that shadows the inherited one, without affecting animal.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
speak() { return `${super.speak()} (woof)`; }
}
typeof Animal; // 'function'
Object.getPrototypeOf(Dog.prototype) === Animal.prototype; // true
Under the hood, methods live on Animal.prototype, Dog.prototype delegates to it, and instances delegate to Dog.prototype. The syntax adds real features (super, #private fields, static blocks, mandatory new), but the inheritance mechanism is still prototype delegation.
| Classical | Prototypal (JS) | |
|---|---|---|
| Inherits from | Classes | Objects |
| Mechanism | Copy structure from the class | Delegate lookups along a live chain |
| Changeable at runtime | Generally no | Yes (add methods to a prototype, Object.setPrototypeOf) |
| Multiple inheritance | Sometimes (C++) or interfaces | Single prototype chain; mix-ins via Object.assign |
| Memory | Methods in a class table | Methods shared on the prototype object |
Array.prototype affects every array in the page.instanceof checks the prototype chain, not a class identity, so it fails across realms (iframes) and can be fooled by Symbol.hasInstance.obj.method as a callback loses this.Object.setPrototypeOf on existing objects deoptimises them in engines. Set the prototype at creation time instead.class in JS is syntax over prototypes, with a few genuine additions.Spotted a mistake or a rights concern? Report an issue