Question presented to candidate: "What is the prototype chain, and what actually happens when you access a property that is not directly on an object?"
What a strong answer should cover:
- Every object has an internal [[Prototype]] link to another object, or to null -- the prototype chain is the full sequence of those links, followed from an object all the way to null.
- When a property is accessed and not found directly on the object, the engine automatically checks the next object up the chain, then the next, until it finds the property or reaches null.
- If the chain is exhausted without finding the property, the result is undefined -- accessing a missing property never throws, it just returns undefined after walking the entire chain.
- The in operator and for...in walk the chain (checking inherited properties too), while Object.prototype.hasOwnProperty and Object.keys only ever look at an object's own properties, ignoring the chain entirely -- a common source of confusion.
- Different kinds of built-in objects have different chain lengths -- a plain object's chain is one hop to Object.prototype then null, while an array's chain is two hops (Array.prototype, then Object.prototype) before null.
Code / implementation expected: Yes -- a runnable snippet walking a real, multi-level chain and showing the in vs hasOwnProperty vs Object.keys distinction with real output.