7.3. Implement Function.prototype.apply
Topics
This question is in
7. this, call, apply and bindfn.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
fn.myApply(thisArg, argsArray) // returns fn's resultthisArg: the valuefnshould see asthis.argsArray: an array or array-like object (anything with alength) whose items become the arguments.nullorundefinedmeans no arguments.- Returns what
fnreturns; iffnthrows, the same error reaches the caller.
Requirements
- Every function has a
myApplymethod that does not appear in afor…inloop, with alengthof 2. - It runs
fnwith the items ofargsArrayas separate arguments and returns its result; a missing,nullorundefinedlist means no arguments. - Any object with a
lengthis a list: arrays,arguments, typed arrays,{ length: 2, 0: "a", 1: "b" }. Thelengthis read once and turned into a whole number the way the language does ("2"is 2,2.7is 2, negative orNaNor missing is 0; a BigInt or a Symbollengththrows aTypeError), then each index from 0 is read in order; a hole or a missing index givesundefined. - A primitive list (a string, number or boolean) throws a
TypeError, andfndoes not run. thisfollows the same rules as the built-in: an object isthis; withnullorundefineda sloppy function sees the global object and a strict oneundefined; a primitive reaches a strict function as it is and a sloppy one wrapped in its object, falsy ones included.- No trace is left on
thisor on any built-in prototype after the call, also whenfnthrows. Whilefnruns,thisgains 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. - It works for built-in functions, for long lists, and when
fncallsmyApplyagain. - Calling
myApplyon something that is not a function throws aTypeError. - It does not use
Function.prototype.call,apply,bind,Reflect.applyorReflect.construct.
Examples
Math.max.myApply(null, [3, 9, 4]); // 9
Array.prototype.join.myApply({ length: 2, 0: "a", 1: "b" }, ["-"]); // "a-b"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 TypeErrorfunction strictSelf() { "use strict"; return this; }
strictSelf.myApply(0, []); // 0
strictSelf.myApply(undefined); // undefinedEnvironment
- Plain JavaScript in a browser page;
apply.jsruns first, then the tests callmyApplyon functions they define in both strict and sloppy code. - The tests replace
call,apply,bind,Reflect.applyandReflect.constructwith versions that report their use, for the momentmyApplyruns. - 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 makethisexactlynull).
Hints
- Problem 7.2 (
call) setsthiswith a method call. Once you have an array of arguments, what is left? Array.fromturns an array-like into an array, but is it the same as readinglengthand 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