JavaScript Prototypes
Every JavaScript object has a hidden link to another object called its prototype. When you read a property that the object doesn't have itself, JavaScript looks it up on the prototype, then on the prototype's prototype, and so on. This prototype chain is how objects share methods — and it's the machinery underneath classes.
Where do toString and push come from?
A fresh object literal has no properties, yet you can call toString() on it. An array has no push property of its own either. Both are found on the prototype:
const point = { x: 1 };
console.log(Object.keys(point)); // own properties only
console.log(typeof point.toString); // found on the prototype
console.log(Object.getPrototypeOf(point) === Object.prototype);
const list = [1, 2];
console.log(Object.hasOwn(list, "push")); // not its own
console.log(Object.getPrototypeOf(list) === Array.prototype);
console.log(Object.getPrototypeOf(Array.prototype) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype)); // end of the chain[ 'x' ]
function
true
false
true
true
null
So the chain for an array is: list → Array.prototype → Object.prototype → null.
Creating objects with a chosen prototype
Object.create(proto) makes a new object whose prototype is proto. Methods placed on the prototype are shared by every object that links to it:
const animal = {
describe() {
return `${this.name} says ${this.sound}`;
},
};
const cat = Object.create(animal);
cat.name = "Mittens";
cat.sound = "meow";
const cow = Object.create(animal);
cow.name = "Bella";
cow.sound = "moo";
console.log(cat.describe());
console.log(cow.describe());
console.log(cat.describe === cow.describe); // one shared function
console.log(Object.getPrototypeOf(cat) === animal);Mittens says meow
Bella says moo
true
true
Inside describe, this is the object the method was called on (cat or cow), even though the function lives on animal.
Shadowing a prototype property
Writing a property always creates it on the object itself. If the prototype has a property with the same name, the object's own one shadows it:
const defaults = { color: "blue", size: "M" };
const shirt = Object.create(defaults);
shirt.color = "red"; // own property, shadows defaults.color
console.log(shirt.color, shirt.size);
console.log(Object.keys(shirt));
delete shirt.color; // remove the own property…
console.log(shirt.color); // …and the prototype's shows throughred M
[ 'color' ]
blue
Constructor functions and prototype
Before classes existed, objects were built with constructor functions called with new. Every function has a prototype property, and new links each created object to it:
function Book(title, pages) {
this.title = title; // own properties of each instance
this.pages = pages;
}
Book.prototype.isLong = function () {
return this.pages > 400;
};
const a = new Book("Short Stories", 180);
const b = new Book("Big Novel", 820);
console.log(a.isLong(), b.isLong());
console.log(Object.getPrototypeOf(b) === Book.prototype);
console.log(b instanceof Book);false true
true
true
Classes are prototypes in disguise
A class does exactly the same thing with cleaner syntax. Methods go on Class.prototype, and extends links one prototype to another:
class Vehicle {
constructor(wheels) {
this.wheels = wheels;
}
info() {
return `${this.wheels} wheels`;
}
}
class Bike extends Vehicle {
constructor() {
super(2);
}
ring() {
return "ring ring";
}
}
const bike = new Bike();
console.log(bike.info(), "/", bike.ring());
console.log(Object.getPrototypeOf(bike) === Bike.prototype);
console.log(Object.getPrototypeOf(Bike.prototype) === Vehicle.prototype);
console.log(typeof Vehicle);2 wheels / ring ring
true
true
function
Inspecting and changing the chain
| Tool | Purpose |
|---|---|
Object.getPrototypeOf(obj) |
read an object's prototype |
Object.create(proto) |
create an object with a given prototype |
Object.setPrototypeOf(obj, proto) |
change it afterwards (slow — avoid) |
obj instanceof Fn |
is Fn.prototype somewhere in obj's chain? |
proto.isPrototypeOf(obj) |
is proto in obj's chain? |
Object.hasOwn(obj, key) |
own property, not inherited? |
obj.__proto__ |
legacy accessor for the prototype — use the methods above instead |
Warning: Never add methods to built-in prototypes such as
Array.prototypein real projects ("monkey-patching"). Your method may clash with future JavaScript features or other libraries.
Test yourself
What happens when you read a property an object doesn't have?
- A ReferenceError is thrown
- JavaScript looks it up along the prototype chain
- It returns null immediately
- The property is created
What is the prototype of Object.prototype?
- Object
- Function.prototype
- null
- itself
Where are the methods of a class stored?
- On each instance
- On ClassName.prototype
- In a global registry
- On Object.prototype