Following Property Lookup
The checkout app’s kits read a kind they don’t own. In this lesson, you follow that read from object to
object, until JavaScript finds it or runs out of objects. In Python, the setup puts the kind on a Defaults class and the inspected flag on an
Inspection subclass, and drilling owns only its code. Python prints tool kit True, and drilling’s own
attributes list only code.
In JavaScript, defaults is a plain object. Object.create() makes a new object linked to the one you
pass it, and reads of anything the new object lacks go there:
const defaults = { kind: "tool kit" };
const inspection = Object.create(defaults);
inspection.inspected = true;
const drilling = Object.create(inspection);
drilling.code = "KIT-17";
console.log(drilling.kind, drilling.inspected);
console.log(Object.keys(drilling));
Watch out: Object.create() copies nothing, so Object.keys() lists only the code:
tool kit true
[ 'code' ]
Draw each object as a box with its own properties and an arrow to the object it links to. The
linked object is its prototype, and the links together are the prototype chain. Reading
drilling.kind checks drilling first, follows the link to inspection, and finds kind one more link along,
on defaults. Object.getPrototypeOf() returns an object’s prototype without changing it, and confirms both links.
For a plain attribute, Python checks the instance’s own attributes, then walks a list of classes, the MRO:
Inspection, Defaults, object. JavaScript has no such list: each object holds one link, to
any object. Defaults and inspection are plain objects, where Python needed two classes.
Defaults has a link too. It leads to Object.prototype, which ends nearly every chain you’ll meet (its misleading
name is the next lesson’s topic). It holds methods every object shares, such as toString(), the way Python’s object gives every class
__repr__. Its own link is null. A property no object has walks the whole chain:
const defaults = { kind: "tool kit" };
const inspection = Object.create(defaults);
inspection.inspected = true;
const drilling = Object.create(inspection);
drilling.code = "KIT-17";
const root = Object.getPrototypeOf(defaults);
console.log(root === Object.prototype);
console.log(Object.getPrototypeOf(root));
console.log(drilling.shelf);
The read of shelf reaches null and gives up quietly:
true
null
undefined
Python raises an AttributeError for the same read. In JavaScript, a mistyped property name reads as
undefined, not an error.
Nothing was copied, so a read always sees the linked objects as they are now. Change the kind on defaults,
and drilling reads the new value right away, as with a Python class attribute. Assigning to drilling.kind
creates an own property that hides the inherited one, as a Python instance attribute does:
const defaults = { kind: "tool kit" };
const inspection = Object.create(defaults);
const drilling = Object.create(inspection);
drilling.code = "KIT-17";
defaults.kind = "loan kit";
console.log(drilling.kind);
drilling.kind = "display kit";
console.log(drilling.kind);
console.log(inspection.kind, defaults.kind);
console.log(Object.keys(drilling));
Drilling logs display kit, inspection and defaults still log loan kit, and drilling now owns kind:
loan kit
display kit
loan kit loan kit
[ 'code', 'kind' ]
That’s for a plain, writable property: an inherited setter runs instead, and a read-only one refuses. Pushing through drilling to an array on defaults is a read first: it finds that array and changes it for every kit, like a list stored as a Python class attribute. So keep per-kit data on the kit.
Classes use the same links: a class builds the linked objects for you. Here, Kit’s describe() returns the kit’s code, and LoanKit extends it and
adds nothing. In Python, describe lives on Kit, not on LoanKit. In JavaScript, follow the links from
drilling with Object.getPrototypeOf():
class Kit {
constructor(code) {
this.code = code;
}
describe() {
return this.code;
}
}
class LoanKit extends Kit {}
const drilling = new LoanKit("KIT-17");
const loanMethods = Object.getPrototypeOf(drilling);
const kitMethods = Object.getPrototypeOf(loanMethods);
console.log(Object.hasOwn(loanMethods, "describe"));
console.log(Object.hasOwn(kitMethods, "describe"));
console.log(drilling.describe());
The first link leads to the object holding LoanKit’s methods, the next to the one holding Kit’s, and only
Kit’s owns describe. The method is found two links up, but this is still drilling, the object the read
started from, as Python’s method gets drilling as self:
false
true
KIT-17
Above Kit’s methods, the chain ends as before: Object.prototype, then null.
So a read checks the object’s own properties, then follows one link at a time, to the name or to null.
Reads are live, and a plain assignment makes an own property. Inspect links with Object.getPrototypeOf(),
and leave them as they are.
Next up is the text lesson Finding the Shared Methods. There, you’ll meet the three objects behind a class, and the one name that trips people up.