A missing property is not necessarily absent. JavaScript first checks an object, then follows its linked [[Prototype]] objects until it finds the property or reaches null. Reading can therefore produce inherited values, while writing usually creates an own property that shadows the inherited one. section 5.11 section 5.3.2
One method, two drawers
const drawerPrototype = { open() { this.isOpen = true; } }; const drawer1 = Object.create(drawerPrototype); const drawer2 = Object.create(drawerPrototype); drawer1.open(); drawer2.open(); Both objects resolve open through the same prototype. During the call, this remains the object that initiated the lookup, so one method operates on separate instance state. section 5.3 section 5.4.2
Use precise operations when reasoning about inherited properties:
- Object.getPrototypeOf(obj) returns the immediate prototype. section 5.3.2
- 'prop' in obj checks the object and its entire prototype chain. section 5.3.2
- Object.hasOwn(obj, 'prop') restricts the check to the object itself; it does not confuse property existence with truthiness. section 5.3.2
- Changing a shared prototype changes behavior for every inheriting object, making dynamic adaptation powerful but potentially hazardous. section 5.3 section 5.11
const drawerPrototype = {
open() { console.log('Opening drawer'); }
};
const drawer1 = Object.create(drawerPrototype);
const drawer2 = Object.create(drawerPrototype);
drawer1.open();
drawer2.open();Inspect a minimal shared-prototype relationship in which two objects resolve the same open method. section 5.3
const drawerPrototype = { open() {} };
const drawer = Object.create(drawerPrototype);
console.log(Object.getPrototypeOf(drawer) === drawerPrototype); #A
console.log(Object.getPrototypeOf(drawerPrototype) ===
Object.prototype); #A
console.log(Object.getPrototypeOf(Object.prototype) === null); #ATrace the chain from an object through Object.prototype to null. section 5.3.2


