JavaScript, inside out · Chapter 24 of 51

The this keyword

By the end of this chapter you can tell what this is in any call, write methods that read their own object, and spot the calls that lose it. 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

Part 4 begins in the kitchen. Ravi wants orders that know their own total, a queue screen for each station, and a counter button that rings the order bell. Anjali wants loyalty cards that can describe themselves. Within an hour of Kabir's first sketch, the kitchen screen crashes with a TypeError about reading station from undefined.

With what you know so far, a function inside an object reaches the object by its variable name. That works for one object. The moment the same method is copied onto a second order, or the card is copied for Ravi, it keeps reading the first one. Methods need a way to say "the object I was called on", and that is this.

What you build: 5 tasks

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

  1. Task 1

    makeOrder: an order whose total reads its own fields

    Write makeOrder(item, price, qty). Return an object with the keys item, price and qty, and a method total() that returns the order's price times its qty: makeOrder('masala chai', 40, 2).total() gives 80.

    total must read the fields through this, not the parameters: after order.qty is changed to 3, total() gives 120, and the same total method copied onto another order (price 25, qty 4) gives 100 for that order.

  2. Task 2

    kitchenQueue.labels: fix the screen that crashes

    kitchenQueue.labels() is broken now: it throws a TypeError. Fix it so it returns one label per order, in order: the station, a colon and a space, then the order, like Counter 1: masala chai.

    labels must read the station and orders of the object it is called on: copied onto another queue with station Counter 2 and the orders Parle-G, it gives Counter 2: Parle-G.

  3. Task 3

    attachBell: fix the button that loses the bell

    attachBell(bell, register) is broken now: the bell's ring method loses its this. register(handler) keeps the handler, and the counter button calls it later as a plain call, handler(). Fix attachBell so that it hands register a function, and every call of that function runs bell's ring with this set to bell: two presses ring the bell twice.

  4. Task 4

    loyaltyCard.describe: fix the card that cannot describe itself

    loyaltyCard.describe() is broken now. Fix it so it returns the card's name, then has, then its stamps and the word stamps: Anjali has 7 stamps.

    It must describe the card it is called on: a copy made with spread and the name Ravi and 3 stamps gives Ravi has 3 stamps.

  5. Task 5 · interview question

    Interview: predictions, what this is in five calls

    Export predictions, an object with the five keys regular, arrow, nested, viaArrow and detached. Each value is a string: what that call in the snippet above it returns, turned into text with String (so undefined is written 'undefined'), or, when the call throws, the error's name, such as 'TypeError'.

    The file is an ES module, so it is strict. The check runs the same snippet and compares.

What you will learn

  • this comes from the call: the object before the dot, decided each time the function runs.
  • Plain calls: no object before the dot, so this is undefined.
  • Arrow functions: no this of their own, they use the one around them.

More in The kitchen

Build The this keyword 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.