Prototypes
Overview
How does [1, 2].map exist when you never added a map property to your array? Through prototypes.
Every object has a hidden link to another object, called its prototype. When you read a property that isn’t on the object itself, JavaScript looks on the prototype, then on its prototype, and so on. That path is the prototype chain.
const list = [1, 2];
Object.getPrototypeOf(list) === Array.prototype; // true
list.hasOwnProperty('map'); // false — it comes from Array.prototypePrototypes are how JavaScript shares behaviour between objects. Instead of copying map, filter and fifty other methods into every array you create, all arrays link to one shared Array.prototype object that holds them.
Creating an object with a prototype
Object.create(proto) makes a new, empty object whose prototype is proto:
const animal = {
speak() {
return `${this.name} makes a sound`;
},
};
const dog = Object.create(animal);
dog.name = 'Bruno';
dog.speak(); // 'Bruno makes a sound' — found on animaldog doesn’t have speak itself; the lookup walks up to animal.
Notice that this inside speak is still dog — this is the object the method was called on, not the object where it was found. That’s what makes shared methods work.
The end of the chain
Almost every chain ends with Object.prototype (which provides toString, hasOwnProperty and friends), and its prototype is null. If a property isn’t found by then, the result is undefined.
Object.getPrototypeOf(Object.prototype); // null
dog.fly; // undefined — searched the whole chainYou can make an object with no prototype using Object.create(null). It has no inherited properties at all, which is occasionally useful for pure dictionaries.
Shadowing
If the object has its own property with the same name, that one wins:
dog.speak = function () { return 'Woof!'; };
dog.speak(); // 'Woof!'
animal.speak.call(dog); // 'Bruno makes a sound' — the original is still thereReading walks up the chain, but writing always puts the property on the object itself. Assigning dog.speak never changes animal.
Own properties versus inherited ones
Object.hasOwn(dog, 'name'); // true — dog's own
Object.hasOwn(dog, 'speak'); // true now (we shadowed it)
'toString' in dog; // true — in checks the whole chain
Object.keys(dog); // ['name', 'speak'] — own, enumerable properties onlyfor…in loops include inherited enumerable properties, which is one reason Object.keys and Object.entries are usually preferred.
Inspecting prototypes
Object.getPrototypeOf(dog) === animal; // true
animal.isPrototypeOf(dog); // true
[] instanceof Array; // true — Array.prototype is in the chainIn the console you’ll see the link displayed as [[Prototype]]. Old code sometimes uses dog.__proto__; it works, but Object.getPrototypeOf is the official way.
Constructor functions
Before class existed, prototypes were usually set up with constructor functions. Every function has a prototype property, and objects created with new link to it:
function Course(title) {
this.title = title;
}
Course.prototype.describe = function () {
return `Course: ${this.title}`;
};
const js = new Course('JavaScript');
js.describe(); // 'Course: JavaScript'
Object.getPrototypeOf(js) === Course.prototype; // trueThis is exactly what a class does behind the scenes, as the next topic shows.
Why it matters
You’ll rarely create prototypes by hand — class does it for you. But knowing the chain explains things like why methods are shared between instances (saving memory), how instanceof works, and what you’re looking at when you expand [[Prototype]] in DevTools.
Don’t extend built-in prototypes such as
Array.prototypein your own code. It can clash with future versions of JavaScript and with libraries.
Share a method
Create a shape object with an area() method that returns this.width * this.height. Use Object.create to make rect with shape as its prototype, give rect its own width and height, and call rect.area().
const shape = {
// add area() here
};
// create rect hereconst shape = {
area() {
return this.width * this.height;
},
};
const rect = Object.create(shape);
rect.width = 4;
rect.height = 3;
rect.area(); // 12Summary
- Every object has a hidden link to another object: its prototype.
- Missing properties are looked up along the prototype chain, which ends at
null. - Writing a property always creates it on the object itself, shadowing the prototype’s.
- Methods on a prototype are shared by every object that links to it.
- Classes are a friendlier syntax over the same mechanism.