JavaScript Prototypes and Inheritance
Understand JavaScript prototypes. Learn how inheritance works under the hood with prototype chain.
JavaScript Prototypes
What are Prototypes?
Every object has a prototype. It's like a parent object that provides properties and methods.
``` ┌─────────────────────┐ │ Object │ │ ┌───────────────┐ │ │ │ name: "John" │ │ │ └───────────────┘ │ │ │ │ │ │ __proto__ │ │ │ │ ┌─────────▼───────────┐ │ Prototype │ │ ┌───────────────┐ │ │ │ toString() │ │ │ │ valueOf() │ │ │ └───────────────┘ │ └─────────────────────┘ ```
Prototype Chain
```javascript const arr = [1, 2, 3];
arr.push(4); // Method from Array prototype arr.toString(); // Method from Object prototype ```
**JavaScript looks up the chain:** ``` Array → Array.prototype → Object.prototype → null ```
Key Takeaway
Prototypes enable inheritance. Objects inherit from prototypes. Prototype chain allows method lookup. Understanding prototypes is key to advanced JavaScript.