JavaScript, inside out · Chapter 46 of 51
Polyfills
By the end of this chapter you can write your own map, filter, reduce and bind, and explain every detail the built-ins handle for you. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 4 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
Tapri is raising its first funding round, and the investors have asked Dev, a CTO they trust, to interview the developer. Dev opens a blank editor: "No libraries, no looking things up. Write map for me." This is the polyfill round. A polyfill is your own version of a built-in, the kind you would ship for an old browser that lacks it. Interviewers love it because it shows whether you know what a built-in really does, not just how to call it.
You have called map, filter, reduce and bind for weeks. But with what you know so far, you would write map as "loop and push fn(item)", and Dev's follow-ups would catch it: the callback also gets the index and the array, a second argument sets this, reduce has two modes, and a bound function can be called with new.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: myMap, your own map
Write myMap(arr, fn, thisArg) without calling the built-in map (the check watches it). For every element, in order, call fn with this set to thisArg and three arguments: the value, its index and the array itself. Return a new array of the results; arr itself must not change.
myMap([40, 25], (p) => p * 2) gives [80, 50], a regular function callback sees thisArg as this, myMap(['1', '2', '3'], parseInt) gives [1, NaN, NaN] just like the built-in, and an empty array gives [].
- Task 2 · interview question
Interview: myFilter, your own filter
Write myFilter(arr, fn, thisArg) without calling the built-in filter (the check watches it). For every element, in order, call fn with this set to thisArg and the value, its index and the array. Return a new array of the elements whose result is truthy (any truthy value counts, such as 2 or 'yes').
Keep the elements themselves, the same objects, not the callback's results. myFilter([40, 25, 35], (p) => p > 30) gives [40, 35], and an empty array gives [].
- Task 3 · interview question
Interview: myReduce, with and without a starting value
Write myReduce(arr, fn, initial) without calling the built-in reduce or reduceRight (the check watches them). Call fn(acc, value, index, arr) for the elements in order, carry its result into the next call, and return the last result.
With a starting value, acc starts there and fn runs for every element from index 0. A starting value of undefined passed on purpose still counts: myReduce([5], fn, undefined) starts from undefined. With no starting value at all, acc starts as arr[0] and fn runs from index 1, so a one-element array gives that element without calling fn. An empty array with a starting value gives the starting value without calling fn, and an empty array with none throws a TypeError.
- Task 4 · interview question
Interview: myBind, your own bind
Write myBind(fn, thisArg, ...preset) without calling the built-in bind (the check watches it). Return a new function. Calling it calls fn with this set to thisArg, the preset arguments first and then the ones it was called with, and returns fn's result: myBind(bill, { name: 'Ravi' }, 2)(40) gives the same as bill.call({ name: 'Ravi' }, 2, 40).
The bound this cannot be changed: not with call, and not by calling it as a method of another object. Called with new, it ignores thisArg and returns a new instance of fn, built with the preset arguments followed by the new ones.
What you will learn
- What the callback gets: value, index and array, and thisArg.
- Writing a polyfill: one loop and fn.call, never the built-in.
- Counting arguments: telling "left out" from "passed undefined".
- What bind remembers: this, preset arguments, and new.
More in The interview
Build "Polyfills" 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.