Mastering JavaScript Prototype Chain: A Complete Guide
This article explains the JavaScript prototype chain in depth, covering the definitions of prototype and __proto__, their relationships, how objects inherit properties, the role of constructors, and includes practical code examples and visual diagrams to illustrate each concept.
Understanding the JavaScript Prototype Chain
Every JavaScript object has an internal [[Prototype]] reference that points to another object, forming a chain that ends with null. This chain is called the prototype chain.
Key Terminology
prototype– a property of functions that points to an object used as the template for instances. __proto__ – the accessor for an object's internal [[Prototype]]; it points to the constructor's prototype object.
All code in this article was verified in Chrome v125.0.6422.142.
1. Prototype Object ( prototype )
Functions are objects and therefore have a unique prototype property. When a function Foo is instantiated as o1 = new Foo(), the instance can access Foo.prototype through its implicit prototype chain.
function Foo() {}
const o1 = new Foo();
Foo.prototype.propA = 'p1';
// o1.propA === 'p1'Adding a property to Foo.prototype makes it available on o1 via the prototype chain.
2. Implicit Prototype ( __proto__ )
Every non‑builtin object has an implicit prototype __proto__ that points to the constructor's prototype object. For the instance o1, o1.__proto__ === Foo.prototype.
o1.__proto__ === Foo.prototype // true
Foo.__proto__ === Function.prototype // true
Function.__proto__ === Function.prototype // trueBuilt‑in constructors such as Function, Date, Array, etc., follow the same pattern, while objects like Math and JSON have prototype set to undefined and their constructor points to Object.
3. Instance Relationships
Testing in Chrome shows:
o1.constructor === Foo // true
Foo.constructor === Function // true
Function.constructor === Function // trueThus: o1 is an object instance of Foo. Foo is both a function and an object, an instance of Function. Function is a self‑referencing function object.
4. Property Ownership Analysis
Objects own __proto__, while functions own prototype. The prototype of a function is an object, which itself has an implicit prototype, creating a multi‑level chain.
5. Implicit Prototype Reference Chain
The implicit prototype of an object points to its constructor's prototype. Consequently, o1.__proto__ === Foo.prototype, Foo.__proto__ === Function.prototype, and Function.__proto__ === Function.prototype.
6. Avoiding Infinite Loops
All function prototype objects have constructors that point to themselves, preventing circular references. For example, Foo.prototype.__proto__ === Object.prototype and Function.prototype.__proto__ === Object.prototype.
Foo.prototype.__proto__ === Object.prototype // true
Function.prototype.__proto__ === Object.prototype // true7. Special Cases for Built‑in Objects
Objects like Math and JSON lack a prototype (it is undefined) and their constructor points to Object.
Math.prototype // undefined
Math.constructor === Object // true
JSON.prototype // undefined
JSON.constructor === Object // true8. Final Prototype Chain Summary
Every object follows the chain: its implicit prototype ( __proto__ ) points to the constructor's prototype , which in turn has its own implicit prototype, and so on, ultimately terminating at null . This chain is known as the prototype chain.
Visual Diagrams
In summary, an object's implicit prototype ( __proto__ ) points to its constructor's prototype, which in turn points to higher‑level prototypes, ultimately reaching null . This linked structure forms the prototype chain that enables property inheritance in JavaScript.
大转转FE
Regularly sharing the team's thoughts and insights on frontend development
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
