Prototypes: how objects share methods
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.
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.
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.
- 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.
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 Dog {constructor(name) {this.name = name;}bark() {return `${this.name} says woof`;}}
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:
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.
- 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.
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.