JavaScript Prototypal Inheritance Explained: How It Works and Differences from Classical Inheritance
everything you need to know about prototypal inheritance in JavaScript, including prototypes, prototype chain, delegation patterns, ES6 classes under the hood, classical vs prototypal differences, use cases, and common pitfalls.
1. Core Concept & Prototype Chain
- Every JavaScript object has an internal
[[Prototype]]
(accessible via__proto__
orObject.getPrototypeOf
). - Prototypal inheritance means objects delegate property access to their prototype object.
2. Creating Inheritance
Object.create
Constructor Functions
ES6 Classes (Syntactic Sugar)
3. Prototypal vs Classical
Aspect | Prototypal Inheritance | Classical Inheritance |
---|---|---|
Mechanism | Objects delegate to other objects | Classes define blueprints and hierarchies |
Syntax | Object.create , __proto__ | class , extends , super |
Dynamics | Highly dynamic; modify at runtime | More static; class definitions fixed |
Instantiation | Delegation-based | Construction via new and constructors |
Flexibility | Mixins & delegation | Structured hierarchy |
4. Use Cases
- Object Composition & Mixins: share behavior across unrelated objects.
- Dynamic Extensions: add or override methods at runtime.
- Polymorphism: delegate to different prototypes for variation.
5. Common Pitfalls
- Overwriting Prototype: resetting a constructor’s
prototype
can break theconstructor
link. - Modifying Built-ins: altering
Array.prototype
orObject.prototype
can lead to global conflicts. - Confusing
__proto__
vsprototype
: the former is on instances, the latter on constructor functions.
6. Summary Table
Feature | Prototypal | Classical |
---|---|---|
Definition | Objects inherit directly via prototype | Classes and instances via constructors |
Prototype Chain | [[Prototype]] link | Class hierarchy under the hood |
Runtime Dynamics | Can change at runtime | Fixed structure after definition |
Syntax Flexibility | Procedural/object-based | Declarative class syntax |
Method Sharing | Shared on prototype object | Shared on class prototype |
Use Object.create for simple delegation patterns.
Favor composition (mixins) over deep inheritance chains.
❌ Avoid modifying built-in prototypes in production.