Skip to curriculum
Jump to the editor

7.3. Implement Function.prototype.apply

Medium
Topics
20–30 min

fn.apply(thisArg, list) is call with the arguments in a list: it runs fn with this set to thisArg and the items of list as separate arguments. Before spread syntax existed, it was how you passed an array to Math.max or borrowed an array method for an array-like object, and it still is in a lot of library code. Rebuilding it means getting this right in strict and sloppy code, and reading the list exactly as the language does: any array-like object works, holes become undefined, and a primitive is refused.

Add myApply to every function, in apply.js. fn.myApply(thisArg, argsArray) must behave like fn.apply(thisArg, argsArray), without using call, apply or bind.

Signature

JAVASCRIPT
fn.myApply(thisArg, argsArray) // returns fn's result
  • thisArg: the value fn should see as this.
  • argsArray: an array or array-like object (anything with a length) whose items become the arguments. null or undefined means no arguments.
  • Returns what fn returns; if fn throws, the same error reaches the caller.

Requirements

  1. Every function has a myApply method that does not appear in a for…in loop, with a length of 2.
  2. It runs fn with the items of argsArray as separate arguments and returns its result; a missing, null or undefined list means no arguments.
  3. Any object with a length is a list: arrays, arguments, typed arrays, { length: 2, 0: "a", 1: "b" }. The length is read once and turned into a whole number the way the language does ("2" is 2, 2.7 is 2, negative or NaN or missing is 0; a BigInt or a Symbol length throws a TypeError), then each index from 0 is read in order; a hole or a missing index gives undefined.
  4. A primitive list (a string, number or boolean) throws a TypeError, and fn does not run.
  5. this follows the same rules as the built-in: an object is this; with null or undefined a sloppy function sees the global object and a strict one undefined; a primitive reaches a strict function as it is and a sloppy one wrapped in its object, falsy ones included.
  6. No trace is left on this or on any built-in prototype after the call, also when fn throws. While fn runs, this gains no string key and no enumerable property; a temporary non-enumerable symbol key may exist, and reflection (Reflect.ownKeys) can see it: without the built-ins, that cannot be avoided.
  7. It works for built-in functions, for long lists, and when fn calls myApply again.
  8. Calling myApply on something that is not a function throws a TypeError.
  9. It does not use Function.prototype.call, apply, bind, Reflect.apply or Reflect.construct.

Examples

JAVASCRIPT
Math.max.myApply(null, [3, 9, 4]);                // 9
Array.prototype.join.myApply({ length: 2, 0: "a", 1: "b" }, ["-"]); // "a-b"
JAVASCRIPT
function count() { return arguments.length; }
count.myApply(null, { length: "3" });  // 3: every argument undefined
count.myApply(null, [1, , 3]);         // 3: the hole is undefined
count.myApply(null);                   // 0
count.myApply(null, "abc");            // throws TypeError
JAVASCRIPT
function strictSelf() { "use strict"; return this; }
strictSelf.myApply(0, []);   // 0
strictSelf.myApply(undefined); // undefined

Environment

  • Plain JavaScript in a browser page; apply.js runs first, then the tests call myApply on functions they define in both strict and sloppy code.
  • The tests replace call, apply, bind, Reflect.apply and Reflect.construct with versions that report their use, for the moment myApply runs.
  • Out of scope: frozen or non-extensible objects, proxies, lists too long for the engine, and a strict-mode function called with null (only the built-ins can make this exactly null).

Hints

  1. Problem 7.2 (call) sets this with a method call. Once you have an array of arguments, what is left?
  2. Array.from turns an array-like into an array, but is it the same as reading length and the indexes yourself? Think of a string.

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…