Skip to content

Prototypes: how objects share methods

Working knowledgeUnder the hood

Two dogs can both bark, yet neither object has a bark property of its own. The method lives on a third object, and every property read walks a chain of links to find it. Here is that walk, step by step, and how class is built on top of it.

01 Two dogs, one bark

Make two objects from a class and ask each one what it owns.

dog.js
class Dog {
constructor(name) {
this.name = name;
}
bark() {
return `${this.name} says woof`;
}
}
const rex = new Dog('Rex');
const fido = new Dog('Fido');
Object.keys(rex); // [ 'name' ]
rex.bark === fido.bark; // true

rex owns name and nothing else, yet rex.bark() works. And both dogs hold the very same function, not two copies. So where is bark, and how does rex reach it?

02 The picture that fails

It is easy to imagine new stamping every method onto every object, like a cookie cutter. That picture predicts Object.keys(rex) would list bark, and that rex.bark === fido.bark would be false. Both predictions are wrong.

Copy methods into each object1,000 dogs, 1,000 barksWhat people often picture. Memory grows with every method on every object, and changing a method later would need every copy updated.
Link each object to a shared one1,000 dogs, 1 barkWhat JavaScript does. Each dog holds its own data and one hidden link to Dog.prototype, where the methods live.

03 What really happens: the lookup walks a chain

Every object has a hidden internal slot called [[Prototype]]. It holds either another object or null. When you read obj.key, the engine checks obj's own properties; if key is not there, it follows the link and checks that object, and so on until it finds key or reaches null.

Three lookups on rex
Step 1 of 10
Links followed 0
rex · own properties
name→'Rex'
Dog.prototype
constructor→Dogbark→function
Object.prototype (3 of many)
toString→functionhasOwnProperty→functionvalueOf→function
Lookups answered
Empty
  • checked, no match
  • match, search stops
  • just answered

rex was made by new Dog('Rex'). It owns one property, name. Its hidden [[Prototype]] link points at Dog.prototype, whose link points at Object.prototype, whose link is null. Nothing has been looked up yet.

— looking up

Engines such as V8 cache where a property was found, so a repeated rex.bark does not really re-walk the chain each time. The result is always the same as the walk above, though, and that walk is what the language defines.

04 class is built on prototypes

class did not add a new object model in ES2015. It is a tidier way to set up the same links. A class is a function; its methods go on that function's prototype object; and new creates an object whose [[Prototype]] points there.

class syntax
class Dog {
constructor(name) {
this.name = name;
}
bark() {
return `${this.name} says woof`;
}
}
The same, by hand
function Dog(name) {
this.name = name;
}
Dog.prototype.bark = function () {
return `${this.name} says woof`;
};

They are not identical in every detail: a class cannot be called without new, its methods are not enumerable, and its body runs in strict mode. But the objects they make have the same shape and the same chain. You can check the links yourself:

JavaScript
console.log(Object.getPrototypeOf(rex) === Dog.prototype);
console.log(Object.getOwnPropertyNames(Dog.prototype));
console.log(Object.getPrototypeOf(Dog.prototype) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
console.log(typeof Dog);
Shell
$ node dog.js
true
[ 'constructor', 'bark' ]
true
null
function

That output is a real run on Node 24. Use Object.getPrototypeOf to read the link. The old __proto__ accessor does the same but is kept only for compatibility.

05 extends adds one more link

A subclass makes the chain one link longer. class Puppy extends Dog sets Puppy.prototype's [[Prototype]] to Dog.prototype, so a puppy finds its own methods first and falls back to dog methods.

JavaScript
class Puppy extends Dog {
play() {
return `${this.name} chases a ball`;
}
}
const bo = new Puppy('Bo');
bo.play(); // found on Puppy.prototype
bo.bark(); // found on Dog.prototype
The chain a read of bo.bark walks, own object first
bo’s prototype chain
bo
Puppy.prototype
Dog.prototype
Object.prototype
null
  • where bark is found

Overriding works because the search stops at the first match: define bark on Puppy.prototype and puppies never reach the dog version. super.bark() is how you skip ahead to it on purpose.

06 Writes stay on the object

Reading walks the chain. Assigning does not: obj.key = value creates or updates an own property of obj, even when the same key exists further up. The own property then shadows the shared one for that object only.

JavaScript
rex.bark = () => 'grr';
rex.bark(); // 'grr' (own property, found first)
fido.bark(); // 'Fido says woof' (still the shared one)
Object.keys(rex); // [ 'name', 'bark' ]

There is one exception. If the key further up the chain is a setter, or a non-writable data property, the assignment goes through the setter (or fails, and throws in strict mode) instead of creating an own property.

To change behaviour for every dog at once, change the shared object: Dog.prototype.bark = … affects rex, fido and dogs not yet made, because they all read through the same link.

07 When it bites

for (const k in obj) send(k);for...in includes inherited keysfor...in walks enumerable properties up the whole chain. Methods from a class are not enumerable, but ones added by assignment or in an object literal used as a prototype are. Use Object.keys or check Object.hasOwn(obj, k).
class Counter { inc = () => this.n++; }Arrow class fields are per instanceA class field is set on each new object, so every counter gets its own inc function. That fixes this but gives up sharing: 1,000 counters hold 1,000 functions.
class Team {} Team.prototype.members = [];Mutable data on the prototypeEvery team reads the same array through the link, so a.members.push(x) changes b.members too. Put per-object data in the constructor.
const dict = Object.create(null); dict.toString; // undefinedObjects with no chainObject.create(null) gives an object whose [[Prototype]] is null. It is handy as a plain dictionary, but dict.hasOwnProperty(k) throws. Use Object.hasOwn(dict, k).
Array.prototype.last = function () {…};Adding to built-in prototypesIt changes every array in the program, and it can clash with a method the language adds later under the same name. Write a plain function instead.
Object.setPrototypeOf(obj, other);Changing the link of a live objectIt works, but MDN warns it is slow in every major engine, which optimise on the assumption that an object's prototype does not change. Choose the prototype when you create the object, with Object.create or class.

08 Check yourself

Question

const a = new Dog('A'); const b = new Dog('B'); Dog.prototype.bark = () => 'hi'; What does a.bark() return now?

Question

rex.bark = () => 'grr'; Which objects now say 'grr'?

Question

rex.fly is not defined anywhere on the chain. What happens when you read it?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?