JavaScript, inside out · Chapter 26 of 51

new and constructor functions

By the end of this chapter you can stamp out many objects of the same shape with a constructor and new, and explain each step new takes. 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

The 4 pm rush brings about two hundred orders, and Ravi's code builds each one by hand with an object literal. Half of them are missing status, and a few have qty spelled quantity. Ravi also wants riders that work even when someone forgets a keyword, and Meera wants one cash drawer for the whole stall, never two. "One blueprint," Kabir says. "Every order comes off the same press."

With what you know so far, you would write each literal by hand, or a function that returns one. Either way the result is a plain object: nothing says it is an order, and nothing links it to methods that all orders share. Classes arrive in two chapters, and they are built on exactly what this chapter teaches.

What you build: 5 tasks

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

  1. Task 1

    ChaiOrder: a constructor that stamps out chai orders

    Write the constructor ChaiOrder(item, price, qty). Called with new, it sets exactly four properties on the new object: item, price, qty (1 when no qty is given) and status, which is always 'queued'. new ChaiOrder('masala chai', 40, 2) gives an order with item 'masala chai', price 40, qty 2 and status 'queued', and it is an instance of ChaiOrder.

    Every new makes a separate order.

  2. Task 2

    Rider: works even when someone forgets new

    Write the constructor Rider(name). It sets exactly two properties on the new object: name, and deliveries, which starts at 0.

    It must also work when called without new: Rider('Sana') returns a proper rider too, an instance of Rider with the same two properties, and a new object every time.

  3. Task 3

    CashDrawer: every new hands back the one real drawer

    Write the constructor CashDrawer(). The first new CashDrawer() sets cash to 0 on the new drawer and remembers that drawer. Every later new CashDrawer() returns that same drawer, so new CashDrawer() === new CashDrawer() is true, and it is an instance of CashDrawer.

    A later new must not reset the cash: after add(40) on the first drawer, a new CashDrawer() still has cash 40. Keep the add method on the prototype as it is.

  4. Task 4

    newResults: predict what new hands back

    Export newResults, an object with the five keys plain, number, object, null and arrow. Each value is what new X().cups gives for the matching constructor in the comment above it: the value itself, a number or undefined, not a string. Keep all five keys, and write undefined itself where the answer is undefined.

    The check makes the same five constructors, runs new on each, and compares strictly.

  5. Task 5 · interview question

    Interview: myNew, the four steps of new

    Write myNew(Constructor, ...args). It does what new Constructor(...args) does, without new and without Reflect.construct: make an empty object linked to Constructor.prototype, run Constructor once with this set to that object and the given arguments, and return the object. The object gets only the properties the constructor sets.

    Step 4: when the constructor returns an object (not null) or a function, return that instead. When it returns a primitive or null, ignore it and return the object you made.

    The check runs the constructor through myNew and fails if it was called with a real new (it reads new.target, which must be undefined).

What you will learn

  • Constructor functions: an ordinary function that fills in this, run with new.
  • The four steps of new: create, link, run, return.
  • Forgetting new: what breaks, and how new.target tells you.

More in The kitchen

Build "new and constructor functions" 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.