Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow the Prototype Chain

In this video, you answer two questions left over from Mei’s Plant class: where the one copy of each shared method lives, and where Goldie’s default .toString() came from before Mei wrote her own. In a short session in Node’s REPL, with a cut-down Plant class, you follow a property lookup link by link, from Goldie up to the end of the chain.

Key Points

  • An own property is one an object holds itself. Object.hasOwn(object, "name") asks about own properties only: Goldie’s only own property is her name, so Object.hasOwn(goldie, "describe") is false, even though goldie.describe() works.
  • Every object has a hidden link, its prototype, to another object, or to null, which marks the end of the chain. Object.getPrototypeOf(goldie) hands you the object at the other end of Goldie’s link: Plant.prototype.
  • A class builds that object. The class makes an object for its plants to link to, puts its methods there, and keeps it in its prototype property, Plant.prototype. That property isn’t the prototype link of Plant itself. new makes each plant’s link when it makes the plant, which is why every Plant shares one copy of each method.
  • Reading a property walks the chain. JavaScript looks on the object first, then follows the links, one prototype at a time, until it finds the property, or reaches null and stops. A method found on a prototype is still called with the object before the dot, so this is still Goldie.
  • The chain for a plant is the instance, then Plant.prototype, then Object.prototype, whose own prototype is null. The default .toString() lives on Object.prototype, and a property no object on the chain has, such as goldie.color, gives undefined.
  • Arrays work the same way. An array, like the lists of records you’ve built since the basics, has no map of its own: its prototype is Array.prototype, where .map() and the other array methods live. That’s why every array has .map().
  • An assignment like Spike’s makes an own property. When Mei assigns Spike his own describe, a function expression, the assignment puts it on Spike and leaves Plant.prototype untouched. Spike’s own describe shadows the shared one, for Spike alone: Goldie still finds the describe on Plant.prototype.
  • in versus Object.hasOwn(): "toString" in goldie is true, because in searches the whole chain, while Object.hasOwn(goldie, "toString") is false, because it looks at Goldie alone.
  • Object.keys() versus for...in: Object.keys(goldie) lists own keys only, [ 'name' ]. A for...in loop can also reach keys on the chain, but only enumerable ones, keys whose enumerable flag says loops should list them. Class methods and the .toString() on Object.prototype aren’t enumerable, so the loop over Goldie skips them and prints name too, the same as Object.keys().

The REPL Session

Run node with no file name, and type each line after the > prompt. The ... lines are Node’s continuation prompts for the class:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Every Plant shares one copy of each method. So where does that copy live? And where did Goldie’s default toString() come from, before Mei wrote her own?

00:10 You’ll follow a lookup, link by link, to answer both.

00:16 Run node with no file name, and type a cut-down Plant class, with a name and a describe method. Mei’s real class has more. Then make Goldie with the new keyword.

00:27 Type goldie, and the REPL shows her data: only her name. There’s no describe in there. Yet calling describe on Goldie still works.

00:39 Object.hasOwn() asks whether an object holds a property itself. For Goldie and describe, it says false. Such a property is an own property, and Goldie’s only one is her name.

00:54 So where does describe come from? Every object has a hidden link, its prototype, to another object, or to null. The class made an object for its plants to link to, and put its methods there.

01:07 It keeps that object in a property called Plant.prototype, which isn’t Plant’s own link. Object.getPrototypeOf() hands you the object at the other end of that link.

01:20 For Goldie, that’s Plant.prototype: true. And Plant.prototype holds describe as its own property. The new keyword made Goldie’s link when it made her.

01:32 When you ask Goldie for describe, JavaScript looks at Goldie first. She has none, so it follows her link up to Plant.prototype, and finds describe there.

01:44 It calls it with Goldie before the dot, so this is still Goldie.

01:50 Now the second question. Goldie’s toString() still gives '[object Object]'. Neither Goldie nor Plant.prototype has one, so the lookup goes further.

02:01 Follow the link from Plant.prototype, and Object.prototype is next. It holds toString() as an own property: true.

02:11 The chain ends there. Object.prototype links to null. Ask for a color, which no object on the chain has, and you get undefined.

02:22 Arrays work the same way. A list of two plant names has no map of its own. Its prototype is Array.prototype, where map and the other array methods live.

02:33 That’s why map works on every array.

02:37 To make her cactus stand out, Mei makes Spike and gives him a describe of his own, a small function that returns Spike the cactus. The REPL echoes the function she assigned.

02:49 Spike’s describe returns Spike the cactus. Your turn: pause the video and predict what describe returns for Goldie now.

03:03 It returns Goldie the plant, unchanged. The assignment made an own property on Spike: true. It didn’t touch Plant.prototype, so every other plant still finds the shared describe.

03:17 For Spike, the lookup finds his own describe first, which hides the shared one. That’s called shadowing.

03:25 The in operator asks about a property too. Is toString() in Goldie? True. Ask Object.hasOwn() the same thing, and it says false.

03:37 Both are right, because they ask different questions. The in operator searches the whole chain. Object.hasOwn() looks at Goldie alone.

03:49 Loops over keys split the same way. Object.keys() lists own keys only: for Goldie, her name. A for...in loop can also reach keys on the chain, but it skips class methods and toString(), so here it lists name too.

04:07 So when you read a property, JavaScript checks the object first. Then it follows the links until it finds the property, or reaches null and stops. An assignment like Spike’s makes an own property on the object.

04:21 For a plant, the instance, Plant.prototype and Object.prototype make up that chain. That’s what a class builds behind the scenes: one shared object of methods, which new links every plant to.

04:36 That’s all you need about prototypes for now. Next, in Exercise: Model Plants With a Class, you’ll write Mei’s Plant class yourself, from a short description.

Become a Member to join the conversation.