Prototypal Inheritance vs Class Inheritance
Prototypal Inheritance vs Class Inheritance in Modern JavaScript
JavaScript's inheritance model is fundamentally different from classical object-oriented languages like Java, C++, or C#. While other languages utilize class-based inheritance (where classes are blueprints that instantiate copies), JavaScript relies on prototypal inheritance (where objects link directly to other objects via references).
1. Classical vs. Prototypal Inheritance
| Dimension | Classical Inheritance (Java, C++) | Prototypal Inheritance (JavaScript) |
|---|---|---|
| Core Entity | Classes are distinct types from instances | Objects inherit directly from other objects |
| Instantiation | Blueprints copied to allocate instance memory | Objects created with links (__proto__) |
| Dynamic Mutation | Class definitions are immutable at runtime | Prototypes can be extended or modified at runtime |
| Taxonomy | Deep, rigid class hierarchies | Flexible delegation chains & composition |
2. The Prototype Chain Mechanics
Every JavaScript object has an internal hidden link to another object, termed its [[Prototype]] (accessible via Object.getPrototypeOf(obj) or obj.__proto__).
When accessing obj.property:
- 1Does
objhave its own property namedproperty? If yes, return it. - 2If not, inspect
obj.[[Prototype]]. - 3If not found, inspect
obj.[[Prototype]].[[Prototype]]... - 4The chain terminates at
Object.prototype.[[Prototype]] === null. If not found, evaluate toundefined.
3. How ES6 Classes Map to Prototypes
ES6 classes are syntactic sugar over prototypes:
4. Checking Prototype Links: instanceof vs isPrototypeOf
Practice Quiz
Mixins & Object Composition
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Private Fields & Methods (#field) | Mixins & Object Composition |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.