JavaScript, inside out · Chapter 25 of 51
call, apply and bind
By the end of this chapter you can choose this yourself: run a function on any object now, or make a new function locked to one. 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 has two kinds of tickets now: kitchen tickets by the stove, and delivery orders from the app. Both need the same one-line summary, and the summary method lives on the kitchen ticket. "Do I copy it?" he asks. Kabir shakes his head: "You borrow it." Ravi also wants the busiest hour from a list of counts, a rider app button that always knows its rider, and receipt lines with the stall and payment method filled in.
With what you know from the last chapter, this is whatever is before the dot. To run the summary on a delivery order you would have to attach the method to the order first, changing an object that is not yours. And a method handed to a button loses its object, exactly as the bell did; the arrow-function wrapper you wrote is the only fix you have.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
deliverySummary: borrow the kitchen ticket's summary with call
Write deliverySummary(order). Run kitchenTicket's summary method once, with this set to the order and 'DELIVERY' as its only argument, and return what it returns: deliverySummary({ id: 1042, items: ['cutting chai', 'bun maska'] }) gives 'DELIVERY #1042: cutting chai, bun maska'.
Use kitchenTicket.summary itself, looked up when deliverySummary runs (the check watches that method), and do not change the order: no summary or any other key may be added to it.
- Task 2
peakOrders: the busiest hour with apply
Write peakOrders(counts). counts is an array of order counts, one per hour. Return the largest one, using Math.max with apply: peakOrders([12, 48, 31]) gives 48, and a single count gives itself.
With no counts at all, return 0 (Math.max with no numbers gives -Infinity).
- Task 3
bindDeliver: a deliver function that never forgets its rider
Write bindDeliver(rider). Return a new function that runs the rider's deliver method with this locked to that rider, however it is called later: as a plain call, as a method of another object, or with call and a different this. Return what deliver returns, like 'Arjun has done 1'.
Every call delivers for the same rider, so Arjun's count goes 1, 2, 3, and a second rider gets a function of their own.
- Task 4
makeReceiptLine: the stall and payment method preset with bind
Write makeReceiptLine(stall, method). Return chargeLine (from the top of the file) bound with this set to stall and its first argument preset to method, so the returned function takes only the amount: makeReceiptLine({ name: 'Tapri Indiranagar' }, 'UPI')(40) gives 'Tapri Indiranagar: UPI ₹40'.
The returned function can be used many times, and calling it with call and another this still uses the stall.
- Task 5 · interview question
Interview: myBind, your own bind
Write myBind(thisArg, ...preset), your own bind, without using the real bind (the check watches it). It is written to live on Function.prototype, so inside it this is the function being bound; the check calls it as myBind.call(fn, thisArg, 'UPI').
If this is not a function, throw a TypeError. Otherwise return a new function. When it is called, it runs the bound function with this set to thisArg and the preset arguments followed by its own, and returns the result. Its this stays thisArg even when it is called with call and another object. It works with no preset arguments too.
What you will learn
- call and apply: run a function now, with the this you choose.
- bind: a new function whose this (and first arguments) are fixed for good.
- How bind works inside: what a hand-written bind must do, the interview favourite.
More in The kitchen
Build "call, apply and bind" 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.