Skip to curriculum
Jump to the editor

7.2. Implement Function.prototype.call

Medium
Topics
20–30 min

fn.call(thisArg, a, b) runs fn with this set to thisArg. It looks simple, and rebuilding it is a classic way to check that you know how this is really decided: by how a function is called. Doing it exactly right also means knowing what call does with null, undefined and primitives, which depends on whether the function is written in strict or sloppy code.

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

Signature

JAVASCRIPT
fn.myCall(thisArg, ...args) // returns fn's result
  • thisArg: the value fn should see as this.
  • args: the arguments for fn.
  • Returns what fn returns; if fn throws, the same error reaches the caller.

Requirements

  1. Every function has a myCall method that does not appear in a for…in loop, with a length of 1 (like the built-in call).
  2. It runs fn with every argument given and returns its result.
  3. An object (including an array, a function or an object with no prototype) given as thisArg is this inside fn.
  4. With null or undefined, a sloppy-mode function sees the global object as this; with undefined, a strict-mode function sees undefined.
  5. With a primitive (a number, string, boolean, symbol or bigint), a strict-mode function sees the primitive itself; a sloppy-mode function sees it wrapped in its object (new Number(5) style).
  6. No trace is left afterwards: while fn runs, this gains no string key and no enumerable property (Object.keys, for…in and JSON.stringify see nothing new); afterwards it keeps exactly its own keys (also when fn throws), its existing properties are untouched whatever their names, and no built-in prototype is left changed. A temporary non-enumerable symbol key may exist while fn runs, and reflection (Reflect.ownKeys, Object.getOwnPropertySymbols) can see it: without the built-ins, that cannot be avoided.
  7. It works when fn calls myCall again, for built-in methods and for class methods; an arrow function keeps its own this.
  8. Calling myCall 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
function greet(greeting, mark) {
  return greeting + ", " + this.name + mark;
}
greet.myCall({ name: "Ana" }, "Hi", "!"); // "Hi, Ana!"
JAVASCRIPT
function sloppyKind() { return typeof this; }
function strictKind() { "use strict"; return typeof this; }

sloppyKind.myCall(7);          // "object": 7 is wrapped
strictKind.myCall(7);          // "number"
sloppyKind.myCall(undefined);  // "object": the global object
strictKind.myCall(undefined);  // "undefined"
JAVASCRIPT
Array.prototype.slice.myCall({ length: 2, 0: "a", 1: "b" }); // ["a", "b"]
Object.prototype.toString.myCall([]);                        // "[object Array]"

Environment

  • Plain JavaScript in a browser page; call.js runs first, then the tests call myCall 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 myCall runs.
  • Out of scope: frozen or non-extensible objects, proxies, and a strict-mode function called with null (only the built-ins can make this exactly null; plain JavaScript can pass undefined at most).

Hints

  1. When does a function see an object as its this without any of the built-ins? Think of obj.method().
  2. How can you add a property to an object that no other code can ever name, or see in Object.keys?
  3. (5).toFixed() works on a primitive. Where does the engine find toFixed?

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…