What are JavaScript Prototypes?
Unlike Java or C++, JavaScript is a prototype-based language. Every object in JavaScript has an internal property called its Prototype. When you try to access a property that doesn't exist on an object, JavaScript looks for it on that object's Prototype, and then on its Prototype's Prototype—this is called the Prototype Chain.
javascript
The Prototype Chain Explained
- Object Instance: The specific object you created.
[[Prototype]]: The internal link to the parent object.Object.prototype: The root of the chain for almost all objects.null: The final end of every prototype chain!
javascript
Constructor Functions & .prototype
Before the
class keyword was introduced in ES6, we used constructor functions to create multiple objects with the same prototype.javascript
Performance Note: Adding a method to the.prototypeis better than adding it inside the constructor. In the prototype, there is only one copy of the function shared by every instance!
Modern Inheritance: extends vs __proto__
While ES6
class syntax is much cleaner, it's actually just "syntactic sugar" for prototypes under the hood.javascript
Summary
| Term | Description |
|---|---|
| [[Prototype]] | The internal link to the parent (not accessible!) |
| proto | The legacy way to access the internal prototype. |
| Object.create(o) | Create a new object with o as its prototype. |
| .prototype | Property on functions used to set inheritance. |
| Object.prototype | The end-level parent for all objects. |
| Best For | Reusable logic, efficient inheritance, memory saving |
| Shallow Link | Modifying a prototype method affects EVERY instance! |
| hasOwnProperty() | Check if property is on the object vs. the prototype |
| getPrototypeOf() | The modern, safe way to check an object's prototype |
| setPrototypeOf() | Change an object's parent in real-time (DANGER!) |
| Shadowing | Property on child "hides" the one on the prototype |
| Functional | No classes needed; just link objects together |
| Legacy | Understanding prototypes is key for debugging old code |
| Constructor | Functions used with a new keyword |
| Instance | An object created from a constructor |
| Method | A function stored on a prototype |
| Lookup | The process of searching through the chain |
| Shared State | Don't store mutable objects on a prototype! |