JavaScript, inside out · Chapter 28 of 51

Classes

By the end of this chapter you can write a class with a constructor, shared methods, per-object fields, private fields and static members, and keep a method's this when it is passed around. 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

Kabir wants the kitchen code moved to classes: orders, the kitchen queue, Anjali's loyalty card and Meera's menu prices. Anjali has a worry: a customer who opens the browser console can type card.stamps = 99 and claim free chai. Ravi has a bug: the counter's order button works when he calls it on the button, and crashes when the till calls it on its own.

With what you know so far, a constructor and its prototype methods are written in separate places, Rider.prototype.assign = function ... line after line. Every property you set is public, so anyone can change the stamps. A value that belongs to all orders, like the GST rate, ends up as a loose variable. And forgetting new still fails in confusing ways.

What you build: 5 tasks

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

  1. Task 1

    Order: the chai order as a class

    Replace the function Order with a class Order (keep the export). Its constructor(item, price, qty) sets exactly four properties: item, price, qty (1 when no qty is given) and status, which is always 'queued'.

    It has a method total() that returns price times qty, shared on Order.prototype (not one copy per order). new Order('masala chai', 40, 2).total() gives 80, and calling Order without new must throw a TypeError.

  2. Task 2

    KitchenQueue: the oldest order is served first

    Fill in the class KitchenQueue. Every queue has its own array in a property called orders (a new array for each queue, never shared).

    add(order) puts the order at the back and returns how many are waiting. next() removes and returns the oldest waiting order, and returns null when the queue is empty. count() returns how many are waiting.

  3. Task 3

    LoyaltyCard: private stamps that redeem at 10

    Fill in the class LoyaltyCard. constructor(owner) sets the public owner, and the stamp count is a private field, #stamps, starting at 0, so a card's only public key is owner and JSON.stringify(card) gives '{"owner":"Anjali"}'.

    stamp() adds one stamp and returns the new count. count() returns the count. redeem() takes 10 stamps away and returns true when there are 10 or more; with fewer it changes nothing and returns false. Setting card.stamps = 99 from outside must not change the count, and every card has its own count.

    static isCard(value) returns true only for an object made by LoyaltyCard; a plain object with owner, stamps and a count method gives false.

  4. Task 4

    Menu: a static GST rate and price helper

    Fill in the class Menu with two static members: GST_RATE, which is 5, and withGst(price), which returns the price plus 5 percent GST, rounded to the nearest rupee. Menu.withGst(40) gives 42, Menu.withGst(25) gives 26 and Menu.withGst(100) gives 105.

    Both belong to the class itself: an object made with new Menu() has neither.

  5. Task 5 · interview question

    Interview: OrderButton, a handler that keeps its this

    OrderButton is broken now: handlePress works when called on the button, but the till keeps the handler in a variable (or pulls it out with destructuring) and calls it on its own, and then it crashes.

    Fix it so handlePress(item) always works on the button it came from, however it is called: it adds one to that button's presses and returns what queue.add(item) returns. Each button counts its own presses.

What you will learn

  • Class syntax: a constructor and its shared methods in one block.
  • Fields: each object's own starting values, declared in the body.
  • Private fields (#name): data only the class can touch.
  • Static members: values and helpers on the class itself.

More in The kitchen

Build "Classes" 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.