intermediate
Prototype chain
Delegated property lookup, own versus inherited properties, and prototype mutation risks.
Objects delegate property lookup through a prototype chain ending at `null`. If a property is not own, the runtime walks `[[Prototype]]`. Methods on prototypes are shared across instances.
`Object.hasOwn(obj, key)` checks own properties. `for...in` includes enumerable inherited keys. Mutating `Array.prototype` is dangerous (prototype pollution).
function Person(name) { this.name = name; }
Person.prototype.speak = function() { return this.name; };
const ada = new Person('Ada');
console.log(Object.hasOwn(ada, 'speak')); // false
On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.
Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.
The trade-off is often clarity versus performance or safety versus convenience.
Checklist:
- Separate own from inherited properties.
- Explain delegation, not copying.
- Never mutate built-in prototypes.