Skip to content

Prototypes

Every JavaScript object has a hidden link to another object called its prototype. When you read a property that the object does not have itself, JavaScript looks for it on the prototype instead. This is how objects share methods without each one holding its own copy, and it is the mechanism underneath classes.

An object literal starts with Object.prototype as its prototype, which is why every object has methods such as toString() and hasOwnProperty() that you never wrote:

const user = { name: 'Priya' };
console.log(user.toString());
console.log(Object.hasOwn(user, 'toString'));
console.log(Object.getPrototypeOf(user) === Object.prototype);

This prints:

[object Object]
false
true

Object.hasOwn() reports whether the object has the property itself, as an own property, and Object.getPrototypeOf() returns the prototype. Object.hasOwn() replaces the older obj.hasOwnProperty(), which fails on objects without Object.prototype in their chain.

Object.create(proto) makes a new, empty object whose prototype is proto. Here user gets greet() from greeter, and this inside the method still refers to user, because this is the object the method was called on:

const greeter = {
greet() {
return `Hello, ${this.name}`;
},
};
const user = Object.create(greeter);
user.name = 'Priya';
console.log(user.greet());
console.log(Object.getPrototypeOf(user) === greeter);
console.log(Object.hasOwn(user, 'greet'));
console.log(Object.keys(user));

This prints:

Hello, Priya
true
false
[ 'name' ]

Object.keys() lists only own properties, so greet does not appear. Object Create ( ) Method covers the second argument, which defines properties with descriptors.

The prototype is a live link, not a copy. A method added to the prototype later is at once available to every object that uses it. Assigning a property on the object itself creates an own property, which hides the prototype’s version for that object only:

const greeter = {
greet() {
return `Hello, ${this.name}`;
},
};
const ana = Object.create(greeter);
ana.name = 'Ana';
const tom = Object.create(greeter);
tom.name = 'Tom';
greeter.farewell = function () {
return `Goodbye, ${this.name}`;
};
console.log(ana.farewell());
console.log(tom.farewell());
tom.greet = () => 'Hi';
console.log(tom.greet());
console.log(ana.greet());

This prints:

Goodbye, Ana
Goodbye, Tom
Hi
Hello, Ana

Object.setPrototypeOf(obj, proto) changes the prototype of an object that already exists:

const greeter = {
greet() {
return `Hello, ${this.name}`;
},
};
const user = { name: 'Sam' };
try {
user.greet();
} catch (err) {
console.log(err.message);
}
Object.setPrototypeOf(user, greeter);
console.log(user.greet());

This prints:

user.greet is not a function
Hello, Sam

It works, but engines optimise objects on the assumption that their prototype does not change, so MDN warns that it can slow down any code that later touches the object. Prefer to give an object the right prototype when you create it, with Object.create() or a class.

Object.create(null) makes an object with no prototype at all. It has none of the built-in methods, which makes it a safe dictionary whose keys can never collide with names such as toString:

const bare = Object.create(null);
bare.colour = 'teal';
console.log(Object.getPrototypeOf(bare));
console.log('toString' in bare);

This prints:

null
false

A prototype can have its own prototype, which forms a chain. Prototype Chain follows that chain step by step.

Steve Griffith covers this topic in his video Introduction to Prototypes in JS.