JavaScript Reference ยท Object
Prototypes and the prototype chain
Concept reference for JavaScript prototypes: how property lookup walks the chain, prototype versus __proto__, classes, shadowing and common pitfalls.
Almost every JavaScript object has a hidden link to another object called its prototype. When you read a property that the object does not have, the engine follows that link and looks on the prototype, then on the prototype's prototype, and so on until it finds the property or reaches null. That sequence of links is the prototype chain, and it is how JavaScript implements inheritance.
Classes, constructor functions and Object.create() are three ways to set up the same mechanism.
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Key ideas
Reading follows the chain, writing does not
Reading obj.x searches the object and then each prototype. Assigning obj.x = 1 normally creates (or updates) an own property on obj itself, even if a prototype has an x. This is called shadowing: the own property hides the inherited one without changing it. The exceptions are inherited setters, which run, and inherited read-only properties, which block the assignment.
prototype versus the actual prototype
Two similar names cause most of the confusion:
| Name | What it is |
|---|---|
Object.getPrototypeOf(obj) |
The real prototype of obj. Also available as the legacy accessor obj.__proto__. |
Fn.prototype |
A property on constructor functions and classes. It is the object that becomes the prototype of instances created with new Fn(). |
So Object.getPrototypeOf(new Dog("x")) === Dog.prototype. Arrow functions and methods have no prototype property.
Where the chain ends
Ordinary objects inherit from Object.prototype, which supplies toString(), hasOwnProperty() and others. Its own prototype is null. Arrays chain through Array.prototype, functions through Function.prototype. Objects made with Object.create(null) have no chain at all.
Classes are prototypes underneath
class B extends A sets B.prototype's prototype to A.prototype, so instance lookups go instance, B.prototype, A.prototype, Object.prototype. Methods live once on the prototype and are shared by all instances.
Usage notes
- Checking where a property lives:
Object.hasOwn(obj, key)for own,key in objfor own or inherited. - Looping.
for...inalso visits inherited enumerable properties;Object.keys()andObject.entries()do not. - Changing it later.
Object.setPrototypeOf()works but is slow; prefer choosing the prototype at creation. - Never modify built-in prototypes such as
Array.prototypein application code. It affects every script on the page and can clash with future language features. - Shared mutable state: putting an array or object on a prototype shares it between all instances. Create such values in the constructor.
instanceofchecks whether a constructor'sprototypeappears in the object's chain.- Cycles are rejected with a
TypeError.
Browser support
Prototypes and Object.getPrototypeOf() work in every browser and JavaScript runtime. class syntax is available in all current browsers.