JavaScript, inside out · Chapter 27 of 51

Prototypes

By the end of this chapter you can make many objects share one copy of their methods, tell an object's own properties from inherited ones, and walk the prototype chain the way instanceof does. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The kitchen
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Ravi's orders are built by a function that returns an object literal with total and describe written inside it. Two hundred orders in the 4 pm rush means two hundred copies of the same two functions. His rider constructor does the same with assign. Kabir wants one shared copy that every order and every rider looks up, and Dev has warned that the interview will ask how instanceof really works.

With what you know so far, the only way to give an object a method is to put the method on that object, once per object. Last chapter showed that new links an object to a prototype, but not what that link does when you read a property, or how to follow it yourself.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    createOrder: orders that share their methods

    Write createOrder(item, price, qty). Return a new object whose prototype is orderMethods itself (not a copy), with exactly three own properties, in this order: item, price and qty.

    total and describe must not be copied onto the order: the order finds them on orderMethods, so every order shares one copy. createOrder('masala chai', 40, 2).total() gives 80 and describe() gives '2 x masala chai'. A method added to orderMethods later must work on orders made earlier.

  2. Task 2

    Rider: one shared assign on the prototype

    Rider works today, but every rider gets its own copy of assign. Change it so a rider has exactly two own properties, name and deliveries (starting at 0), and assign lives on Rider.prototype, one function shared by every rider.

    assign(orderId) adds one to that rider's deliveries and returns text like 'Arjun is taking order 1042'. Assigning to Arjun twice gives him 2 deliveries and leaves Sana's at 0.

  3. Task 3

    whereIs: own, inherited or missing

    Write whereIs(obj, key). Return the text 'own' when the object itself has the key, 'inherited' when the key is found further up its prototype chain, and 'missing' when it is nowhere on the chain.

    A key whose value is undefined still counts as own. toString on an ordinary object is inherited. It must also work on an object made with Object.create(null), which has no hasOwnProperty method: there, toString is missing.

  4. Task 4

    prototypeChain: every prototype, nearest first

    Write prototypeChain(obj). Return an array of every prototype of obj, nearest first: its prototype, that one's prototype, and so on, stopping when the next one is null (null is not in the array). The items are the prototype objects themselves.

    prototypeChain([]) gives [Array.prototype, Object.prototype], a function's chain starts with Function.prototype, and an object made with Object.create(null) gives [].

  5. Task 5 · interview question

    Interview: myInstanceOf, instanceof by walking the chain

    Write myInstanceOf(value, Constructor). Return true when Constructor.prototype is anywhere on value's prototype chain, false otherwise, the way value instanceof Constructor does, but without using instanceof.

    A primitive ('chai', 40), null and undefined give false. A function is an object, so an arrow function is an instance of Function. An object from Object.create(null) is not an instance of Object. The chain is read when you call, so a prototype changed later counts.

    Careful: a class can change what instanceof answers for it, and the check uses one that answers true for everything. Your walk must ignore that and still give false.

What you will learn

  • The prototype chain: where JavaScript looks when a property is not on the object itself.
  • Object.create and Object.getPrototypeOf: make the link, and follow it.
  • Own or inherited: Object.hasOwn against in.
  • How instanceof decides: a walk up the chain.

More in The kitchen

Build "Prototypes" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.