The prototype chain
Every JavaScript object has a hidden link to another object, its prototype. A read that finds nothing on the object follows the link and tries the next one, and it keeps going until a link is null. That walk is the whole of inheritance in this language, and classes are a shorter way to write it.
This lesson builds the chain by hand with Object.create, walks it with Object.getPrototypeOf, separates the keys an object owns from the ones it borrows, and shows where the walk stops. The next lesson puts a constructor function at the top of a chain, and the one after that replaces the handwork with a class.
A prototype is another object
Object.create makes a new object and sets its prototype to the argument, so the new object borrows every key of the old one without copying any. The borrow is a read-through: a key that the object owns hides the inherited one, and a key the object does not own is read from up the chain. A method on the prototype therefore works for every object that inherits from it.
The file chain.mjs:
const animal = {
eats: true,
describe() {
return `${this.name} eats: ${this.eats}`;
},
};
const rabbit = Object.create(animal);
rabbit.name = 'Rabbit';
rabbit.jumps = true;
console.log('an own field:', rabbit.jumps);
console.log('an inherited field:', rabbit.eats);
console.log('an inherited method:', rabbit.describe());
console.log('the own keys:', Object.keys(rabbit).join(','));
console.log('the prototype is another object:', Object.getPrototypeOf(rabbit) === animal);
node chain.mjs
an own field: true
an inherited field: true
an inherited method: Rabbit eats: true
the own keys: name,jumps
the prototype is another object: true
Two own fields and three answers that came from the prototype, including a method that read this.name from the object the call went through. The prototype holds nothing of the rabbit, and the rabbit holds nothing of the animal: the link is the only connection. The list of keys shows the own fields only, because a list of keys is a list of what the object holds itself.
Walking the chain
The link is readable, so a loop can follow it to the end. Object.getPrototypeOf returns the next object, and null means the chain has no more steps. Object.prototype.isPrototypeOf answers whether the walk from an object reaches another one, which is a different question from the equality of two links.
The file walk.mjs:
const base = { kind: 'base' };
const middle = Object.create(base);
const top = Object.create(middle);
const names = [];
let current = top;
while (current !== null) {
names.push(current === top ? 'top' : current === middle ? 'middle' : current === base ? 'base' : 'object');
current = Object.getPrototypeOf(current);
}
const isPrototypeOf = Object.prototype.isPrototypeOf;
console.log('the chain:', names.join(' <- '));
console.log('a lookup walks it:', top.kind);
console.log('the nearest prototype answers first:', Object.getPrototypeOf(top) === middle);
console.log('is base a prototype of top:', isPrototypeOf.call(base, top));
console.log('is top a prototype of base:', isPrototypeOf.call(top, base));
node walk.mjs
the chain: top <- middle <- base <- object
a lookup walks it: base
the nearest prototype answers first: true
is base a prototype of top: true
is top a prototype of base: false
The loop named every object it passed, and the last name is the prototype every literal has. The call form of isPrototypeOf is not a formality: a table built with Object.create(null) has no such method, and the eslint rule that asks for this form exists for that reason. The two answers at the end are the asymmetry of the relation: a chain has a direction, and the top is not a prototype of the bottom.
Own and inherited keys
Object.keys lists the own enumerable keys, Object.hasOwn answers about one of them, and a for-in loop is the odd one out: it visits the inherited enumerable keys as well. That is the loop to avoid for data and the loop to use when a walk over a prototype is what the program means.
The file own.mjs:
const proto = {
kind: 'proto',
greet() {
return 'hello from the prototype';
},
};
const child = Object.create(proto);
child.name = 'child';
console.log('the inherited method:', child.greet());
console.log('the own keys:', Object.keys(child).join(','));
console.log('Object.hasOwn:', Object.hasOwn(child, 'name'), Object.hasOwn(child, 'greet'));
console.log('in sees the chain:', 'greet' in child);
const seen = [];
for (const key in child) {
seen.push(key);
}
console.log('a for-in loop sees both:', seen.join(','));
child.greet = () => 'a method of its own';
console.log('a shadowing method wins:', child.greet());
console.log('now the own keys:', Object.keys(child).join(','));
node own.mjs
the inherited method: hello from the prototype
the own keys: name
Object.hasOwn: true false
in sees the chain: true
a for-in loop sees both: name,kind,greet
a shadowing method wins: a method of its own
now the own keys: name,greet
The loop saw three names where the object owns one, which is the reason a for-in loop over data needs a guard. The last two lines are the shadow: a write with the same name as an inherited method creates an own method and hides the inherited one, without touching the prototype. Nothing was deleted, so another object that inherits from the same prototype still gets the old method.
Where the chain ends
A literal inherits from Object.prototype, and that object inherits from nothing, so the walk has two steps at most. An array has one more: Array.prototype first, then Object.prototype. A table made with Object.create(null) skips the whole thing, which is why it has no toString and no hasOwnProperty.
The file ends.mjs:
const literal = {};
console.log('a literal inherits from Object.prototype:', Object.getPrototypeOf(literal) === Object.prototype);
console.log('Object.prototype has no prototype:', Object.getPrototypeOf(Object.prototype));
console.log('so a name from it still answers here:', typeof literal.toString);
const table = Object.create(null);
console.log('a table with no prototype:', Object.getPrototypeOf(table));
console.log('and nothing is inherited there:', typeof table.toString);
const list = [];
console.log('an array has one more step:', Object.getPrototypeOf(list) === Array.prototype);
console.log('and the chain goes on:', Object.getPrototypeOf(Array.prototype) === Object.prototype);
node ends.mjs
a literal inherits from Object.prototype: true
Object.prototype has no prototype: null
so a name from it still answers here: function
a table with no prototype: null
and nothing is inherited there: undefined
an array has one more step: true
and the chain goes on: true
The null at the end of the first chain is the value that stops every lookup, and the function found on the literal came from the step before it. The table with no prototype answered with a null prototype and nothing else, which the lesson on objects as maps uses as a safe home for keys from data. The array shows that a chain can have more than two steps, and that every built-in type adds its own.
Exercises
- Add a fly() method to the animal in chain.mjs and call it on the rabbit. Does the method need the rabbit to declare anything?
- Print the chain of a function and of a Map instance in walk.mjs. How many steps does each one have before null?
- Change the loop in own.mjs to a for-of over Object.keys and count the names. Which key of the three disappears, and why?
- Set the prototype of a literal to null with Object.setPrototypeOf and call toString on it. What happens?
- Compare Object.create(proto) with { ...proto } in own.mjs. Which of the two shares a change made to the prototype afterwards?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a lookup do when the key is not on the object itself?
Answer
It reads the next object in the prototype chain
The chain is part of every read, and the walk stops at the first object that has the key.
-
What does
Object.create(proto)do?Answer
It makes a new object whose prototype is proto
The new object borrows the keys through the link and copies none of them.
-
Which call answers whether an object owns a key itself?
Answer
Object.hasOwn(object, "key")
in walks the chain, a read can find an inherited key, and only hasOwn asks about the object.
-
Which loop visits inherited enumerable keys as well?
Answer
for-in
for-in is the only one of the four that follows the chain.
-
What does this program print?
const proto = { a: 1 }; const o = Object.create(proto); o.b = 2; console.log(o.a, o.b, Object.keys(o).join(","));Answer
1 2 b
The inherited key is readable and the list of own keys has only the one that was set.
-
What does this program print?
const proto = { greet() { return "hi"; } }; const o = Object.create(proto); console.log(Object.hasOwn(o, "greet"), o.greet());Answer
false hi
The method is on the prototype, so the object does not own it, and the call still finds it.
-
What does this program print?
const proto = { a: 1 }; const o = Object.create(proto); o.a = 2; console.log(proto.a, o.a);Answer
1 2
The write created an own key and left the inherited one alone.
-
What does this program print?
console.log(Object.getPrototypeOf(Object.prototype));Answer
null
Object.prototype is the last object of the chain, and its prototype is null.
-
What does this program print?
const table = Object.create(null); console.log(typeof table.toString, typeof table.hasOwnProperty);Answer
undefined undefined
A null prototype inherits nothing, so neither name is there.
-
What does this program print?
const proto = { kind: "proto" }; const o = Object.create(proto); const seen = []; for (const key in o) seen.push(key); console.log(seen.join(","));Answer
kind
for-in follows the chain, so the inherited enumerable key is in the list even though the object owns none.
The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev