Sub topic 11
The Prototype Chain and Inherited Properties
This chapter assumes you can create objects, read properties, and check for them with in and Object.hasOwn. It also assumes you know how this works in methods and how for...in and Object.keys list properties. Earlier chapters showed that in can see properties the object does not own. This chapter explains where those properties come from.
The working model
Every object has a hidden link to another object, called its prototype, or to null. When you read a property, JavaScript checks the object's own properties first. If the property is not there, it checks the prototype, then that object's prototype, and so on. This sequence of linked objects is the prototype chain. The lookup stops at the first match. If it reaches null without a match, the result is undefined.
An object literal's prototype is Object.prototype. That is why every plain object has methods like toString and hasOwnProperty without defining them.
Creating objects with a chosen prototype
Object.create(proto) makes a new empty object whose prototype is proto. Object.getPrototypeOf(obj) returns an object's prototype.
const animal = {
eats: true,
describe() {
return `${this.name} eats: ${this.eats}`;
}
};
const rabbit = Object.create(animal);
rabbit.name = "Rabbit";
console.log(rabbit.eats); // true (inherited)
console.log(rabbit.describe()); // "Rabbit eats: true"
console.log(Object.getPrototypeOf(rabbit) === animal); // true
console.log(Object.getPrototypeOf(animal) === Object.prototype); // true
console.log(Object.getPrototypeOf(Object.prototype)); // nulldescribe is found on animal, but this is still rabbit, because this depends on how a method is called, not on where it is found. Inherited methods can therefore work with each object's own data.
Reads go up the chain, writes stay local
When you assign a property, the chain is not used for the write. The assignment creates or updates an own property on the object itself. This hides the inherited property with the same name, which is called shadowing.
const animal = { eats: true };
const rabbit = Object.create(animal);
rabbit.eats = false;
console.log(rabbit.eats); // false (own property)
console.log(animal.eats); // true (unchanged)
console.log(Object.hasOwn(rabbit, "eats")); // true
delete rabbit.eats;
console.log(rabbit.eats); // true (inherited value visible again)delete only removes own properties. You cannot delete an inherited property through the object that inherits it.
Common errors
Mutating a shared object on the prototype
Assigning a property shadows it, but calling a mutating method on an inherited value does not. The method changes the object stored on the prototype, so every object that inherits it sees the change.
const base = { tags: [] };
const a = Object.create(base);
const b = Object.create(base);
a.tags.push("x"); // reads base.tags, then mutates it
console.log(b.tags); // ["x"]: shared through the prototype
a.tags = ["y"]; // assignment creates an own property on a
console.log(b.tags); // ["x"]Keep mutable state as own properties on each object, and put only shared behavior, such as methods, on the prototype.
Expecting enumeration to skip inherited keys
for...in lists enumerable inherited properties after the object's own ones. Object.keys, Object.values, and Object.entries list only own properties.
const animal = { eats: true };
const rabbit = Object.create(animal);
rabbit.name = "Rabbit";
for (const key in rabbit) {
console.log(key); // "name", then "eats"
}
console.log(Object.keys(rabbit)); // ["name"]Inherited properties like toString don't appear in for...in because they are non-enumerable. The next chapter covers that attribute.
Exercise
Given const p = { x: 1 }; const c = Object.create(p); c.x = 2; delete c.x; p.x = 3;, what do c.x and Object.hasOwn(c, "x") return, and why?
Show answer
c.x is 3 and Object.hasOwn(c, "x") is false. Assigning c.x = 2 created an own property that shadowed p.x. Deleting it removed that own property, so the lookup falls through to p, which now holds 3.
Exercise
Why does rabbit.describe() return a string that uses rabbit.name even though describe is defined on animal?
Show answer
The lookup finds describe on animal, but the method is called as rabbit.describe(), so this is bound to rabbit. Reading this.name then finds rabbit's own name property.