In this video, you find out which .describe() the toy library’s shelf loop runs for each kind of toy, and where super.describe() starts looking. You follow each call on the 1,000-brick set up its prototype chain, one link at a time, then do the same for the classes themselves with fromRecord().
Key Points
- A building set has two links above it.
brickslinks toBuildingSet.prototype, where the class keeps its methods, andextendslinksBuildingSet.prototypeup toToy.prototype..describe()isn’t an own property ofbricks, soObject.hasOwn(bricks, "describe")isfalse. - Look on the object first, then take the first match up the chain.
bricks.describe()finds the override onBuildingSet.prototypeand stops there, so the line ends with the bricks.bricks.suitsAge(9)finds nothing onbricksorBuildingSet.prototype, and climbs one more link toToy.prototype. - A parent’s method that calls
this.describe()gets the child’s version. The new.label()method inToyis found onToy.prototypeand called withbricksasthis, sothis.describe()inside it starts a new lookup atbricksand finds theBuildingSetoverride first. superstarts one level up. Inside theBuildingSetoverride,super.describe()starts atToy.prototype, one link aboveBuildingSet.prototype, where the override is written. It finds theToyversion, not the override again, andthisstill points tobricks.instanceofwalks the same chain.bricks instanceof BuildingSetandbricks instanceof Toyare bothtrue, becauseBuildingSet.prototypeandToy.prototypeare both on the chain frombricks.bricks instanceof DressUpisfalse, sinceDressUp.prototypeisn’t on that chain.- Classes have a chain too. A class’s
.prototypeproperty is where its instances link. The class’s own prototype is a separate link, andextendspoints it at the classToy. SoBuildingSet.fromRecord(record)finds the static method onToyone link up. new this(...)builds the class the call started from. In a static method,thisis the class before the dot. Withnew Toy(...),BuildingSet.fromRecord()built a plainToy. Withnew this(...), it builds aBuildingSet.- A subclass with its own properties overrides
fromRecord(). TheToyversion only passes the values every toy has, so untilBuildingSethas its ownfromRecord(), the set’s brick count isundefined. The staticfromRecord()onBuildingSetcallssuper.fromRecord(record). As withsuper.describe(),superonly moves the start of the lookup up toToy. The keywordthisstaysBuildingSet, sonew this(...)still builds a set. Then it adds the brick count. - Built-in classes have chains too. A class can extend
Map, and its instances are still maps. Keep your own families to one or two levels, so you can follow every lookup by eye.
The Final Code
toy.js, with the new .label() method and fromRecord() building new this(...):
toy.js
export class Toy {
#minAge;
constructor(id, name, minAge) {
this.id = id;
this.name = name;
this.#minAge = minAge;
}
static fromRecord(record) {
return new this(record.id, record.name, record.minAge);
}
suitsAge(age) {
return age >= this.#minAge;
}
describe() {
return `${this.id}: ${this.name} (ages ${this.#minAge}+)`;
}
label() {
return `Shelf label: ${this.describe()}`;
}
}
building-set.js, with its own static fromRecord():
building-set.js
import { Toy } from "./toy.js";
export class BuildingSet extends Toy {
constructor(id, name, minAge, brickCount) {
super(id, name, minAge);
this.brickCount = brickCount;
}
static fromRecord(record) {
const set = super.fromRecord(record);
set.brickCount = record.brickCount;
return set;
}
bagsNeeded() {
return Math.ceil(this.brickCount / 250);
}
describe() {
return `${super.describe()}, ${this.brickCount} bricks`;
}
}
dress-up.js is unchanged from the last lesson:
dress-up.js
import { Toy } from "./toy.js";
export class DressUp extends Toy {
constructor(id, name, minAge, size) {
super(id, name, minAge);
this.size = size;
}
describe() {
return `${super.describe()}, size ${this.size}`;
}
}
chain.js follows the instance chain from bricks, one link at a time:
chain.js
import { Toy } from "./toy.js";
import { BuildingSet } from "./building-set.js";
import { DressUp } from "./dress-up.js";
const bricks = new BuildingSet("T02", "Building set", 8, 1000);
console.log("Own describe?", Object.hasOwn(bricks, "describe"));
console.log("Link 1:", Object.getPrototypeOf(bricks) === BuildingSet.prototype);
console.log("Link 2:", Object.getPrototypeOf(BuildingSet.prototype) === Toy.prototype);
console.log(bricks.describe());
console.log("Suits Priya (9):", bricks.suitsAge(9));
console.log(bricks.label());
console.log("A BuildingSet?", bricks instanceof BuildingSet);
console.log("A Toy?", bricks instanceof Toy);
console.log("A DressUp?", bricks instanceof DressUp);
Run it with Node, and each line answers one question about the chain:
$ node chain.js
Own describe? false
Link 1: true
Link 2: true
T02: Building set (ages 8+), 1000 bricks
Suits Priya (9): true
Shelf label: T02: Building set (ages 8+), 1000 bricks
A BuildingSet? true
A Toy? true
A DressUp? false
records.js follows the classes’ chain:
records.js
import { Toy } from "./toy.js";
import { BuildingSet } from "./building-set.js";
const record = { id: "T02", name: "Building set", minAge: 8, brickCount: 1000 };
console.log("Own fromRecord?", Object.hasOwn(BuildingSet, "fromRecord"));
console.log("Link:", Object.getPrototypeOf(BuildingSet) === Toy);
const bricks = BuildingSet.fromRecord(record);
console.log("A BuildingSet?", bricks instanceof BuildingSet);
console.log(bricks.describe());
With the final toy.js and building-set.js, it prints:
$ node records.js
Own fromRecord? true
Link: true
A BuildingSet? true
T02: Building set (ages 8+), 1000 bricks
Next, in Exercise: Add a New Kind of Toy, you’ll add the library’s first musical toy to the family, with the batteries it takes and a note for the volunteers to tape to its box. The code runs right here on the page, and automated tests check it each time you run it, so run your code early and often.