Loading video player…

Following Property Lookup

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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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():

Language: JavaScript
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:

Language: Program Output
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.

00:00 The checkout app’s kits read a kind they don’t own. Follow that read from object to object, until JavaScript finds it or runs out of objects. In Python, Defaults holds the kind, Inspection adds an inspected flag, and drilling owns only its code.

00:18 Python prints tool kit and True, and drilling’s own attributes list only the code. In JavaScript, defaults is a plain object. Object.create() makes a new object linked to the one you pass it.

00:33 Reads of anything the new object lacks go there. So inspection links to defaults and owns the flag; drilling links to inspection and owns its code. JavaScript logs tool kit and true.

00:47 Watch out: Object.create() copies nothing, so Object.keys() lists only the code.

00:55 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.

01:07 The read checks drilling first: it owns only the code. So it follows the link to inspection: no kind there either. One more link, and defaults has it: tool kit.

01:20 Object.getPrototypeOf() returns an object’s prototype without changing it. Both checks log true.

01:28 For a plain attribute, Python checks drilling’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.

01:45 Defaults and inspection are plain objects, where Python needed two classes.

01:51 Defaults has a link too. Read it into root, and compare it with Object.prototype. It logs true. Object.prototype ends nearly every chain you’ll meet.

02:03 Its name is misleading, and the next lesson untangles it. The object holds methods every object shares, such as toString(), the way Python’s object gives every class __repr__.

02:14 Its own link is null: there’s nothing after it. Now, what does drilling.shelf log? Your turn: pause the video and predict.

02:30 It logs undefined.

02:33 The read walked all four objects, reached null, and gave up quietly. Python raises an AttributeError for the same read. So in JavaScript, a mistyped property name reads as undefined, not an error.

02:49 Nothing was copied, so a read always sees the linked objects as they are now. Change the kind on defaults to loan kit, and drilling reads loan kit right away, as a Python class attribute does.

03:03 Assigning display kit to drilling.kind creates an own property, which hides the inherited one. Drilling logs display kit, while inspection and defaults still log loan kit.

03:15 Object.keys() now lists code and kind. Python’s instance attribute shadows its class attribute the same way. That’s for a plain, writable property: an inherited setter runs instead, and a read-only one refuses.

03:31 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.

03:41 So keep per-kit data on the kit.

03:46 Classes use the same links; a class builds the linked objects for you. Kit’s describe method returns the code, and LoanKit extends it, adding nothing.

03:56 In Python, describe lives on Kit, not on LoanKit, and drilling’s describe prints KIT-17. In JavaScript, follow drilling’s links with Object.getPrototypeOf().

04:10 The first link leads to the object holding LoanKit’s methods. The next leads to the one holding Kit’s. Object.hasOwn() finds describe only on Kit’s methods: false, then true.

04:24 So drilling.describe() is found two links up, but this is still drilling. It logs KIT-17, as Python’s method gets drilling as self.

04:35 Above Kit’s methods, the chain ends as before.

04:40 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.

04:52 Inspect links with Object.getPrototypeOf(), and leave them as they are.

04:57 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.

Become a Member to join the conversation.