Read a key, watch JS walk the [[Prototype]] hop by hop with Object.hasOwn until it is found or null. Shadowing, for...in, a circular chain. An interactive…
Why it matters
Almost all inheritance in JavaScript, class included, runs through this chain: array methods, DOM methods, the toString on every object. Knowing how the walk goes explains why methods seem to come from nowhere, why for...in brings inherited keys along, why a mistyped property name becomes undefined, and why changing a built-in prototype is dangerous.
The key idea
Reading a property is a walk, not a copy
Common mistakes
Thinking a prototype’s methods are "copied" onto an object when it is made No: every read walks the chain right then. A method added to the prototype after the object was made is still read, by every object that inherits from it.
for...in brings keys the object does not ownfor...in climbs into the prototype and walks the enumerable inherited keys. Filter with Object.hasOwn(obj, k), or use Object.keys(obj), which takes only own keys.
Adding a method to Array.prototype or Object.prototype for convenience That changes every array or object on the whole page, other libraries’ too: a source of global bugs. This counter refuses to change Object.prototype too. Write your own function or class.
Try it in this playground
Read an inherited key: user.sapa Found one cabinet up.
Read a missing key: user.bogus Up to null: undefined, not an error.
Hide an inherited drawer with an own one Shadowing: the walk stops at the first cabinet.
Add a drawer to the prototype, then read it from user Seen at once: nothing was copied.