liveBook · Manning

Property lookup

As explained in The Joy of JavaScript

The process by which a language determines which property value or method is returned when a property access occurs.

Property lookup determines which value JavaScript returns when code reads an object property or invokes a method. The engine checks the object first. If the property is absent, it follows the object’s internal [[Prototype]] link and repeats the search up the prototype chain. A local property wins; an inherited property remains available through the same access syntax. section 2.1.1 section 1.2

A property found on a prototype

const transaction = { sender: 'luis@tjoj.com' }; const moneyTransaction = Object.create(transaction); moneyTransaction.funds = 10; moneyTransaction.funds; // 10: found locally moneyTransaction.sender; // 'luis@tjoj.com': found on transaction Object.create links the new object to transaction; it does not need to copy sender onto the new object. section 2.1 section 2.1.1

Lookup continues through linked objects as far as Object.prototype. If no value is found, a property read yields undefined; attempting to invoke a missing method produces a TypeError. Use Object.create to establish a link and Object.getPrototypeOf to inspect it rather than manipulating __proto__ directly. section 2.1.1 section 2.1

The same mechanism lets constructor-created instances share methods through their prototypes. An incorrectly configured prototype chain can therefore make a method appear to be missing even when it was defined on a constructor’s prototype. Linked objects can also specialize a parent by adding properties or overriding inherited ones; changes higher in a chain can affect derived objects at runtime. section 2.2.2 section 2.1.2 section 1.2

Module design offers a useful contrast: calls on a CommonJS module object use ordinary property resolution, whereas the static structure of an ECMAScript module allows named lookups to be resolved ahead of time. section 6.5.2

How The Joy of JavaScript covers it

How deep it goes
Traces lookup from an object through [[Prototype]] to Object.prototype, including failure behavior; leaves shadowing largely unexplored.
Examples
Uses linked transaction objects to contrast local and inherited properties, then a missing constructor-prototype method to expose a broken chain.
What it assumes
Assumes familiarity with JavaScript objects and some exposure to prototypes while building the lookup process from a basic linked-object example.
What you can do after
Diagnose/debug missing inherited properties or methods by checking where a lookup starts and how prototype links are configured.
How it connects
Connects prototype chains, differential inheritance, constructor functions, shared methods, and module imports.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.