Skip to curriculum
Jump to the editor

7.6. Predict this in eight calls

Medium
Topics
15–25 min

Most bugs with this come from one wrong assumption: that this belongs to where a function is written. For regular functions, it is decided by how the function is called; arrow functions take it from where they are written; classes and strict mode change what a plain call gives. Interviews test this with short snippets, and so does every callback you pass around.

Below are eight calls. For each one, predict what who(this) returns when the snippet runs as an ordinary (sloppy) script, and when it runs as a strict script, with "use strict"; as its first line. Write your answers in predict.js. The tests run every snippet for real, both ways, and compare.

Files

predict.js holds one object:

JAVASCRIPT
const answers = {
  a: { sloppy: "?", strict: "?" },
  // … b to h
};

Every snippet uses this helper, which turns this into a word:

JAVASCRIPT
function who(self) {
  if (self === undefined) return "undefined";
  if (self === null) return "null";
  if (self === globalThis) return "globalThis";
  if (typeof self === "number") return "number";
  if (self instanceof Number) return "Number object";
  return self.label;
}

The eight calls

Each snippet is a separate script. The answer is what its last line returns.

a.

JAVASCRIPT
const user = { label: "user", show() { return who(this); } };
user.show();

b.

JAVASCRIPT
const user = { label: "user", show() { return who(this); } };
const show = user.show;
show();

c.

JAVASCRIPT
const user = { label: "user", show: () => who(this) };
user.show();

d.

JAVASCRIPT
const timer = {
  label: "timer",
  start() { return [1].map(() => who(this))[0]; },
};
timer.start();

e.

JAVASCRIPT
function kind() { return who(this); }
kind.call(7);

f.

JAVASCRIPT
class Counter {
  show() { return who(this); }
}
const show = new Counter().show;
show();

g.

JAVASCRIPT
const user = { label: "user", show() { return who(this); } };
const bound = user.show.bind({ label: "first" });
bound.call({ label: "second" });

h.

JAVASCRIPT
function Made() {
  this.label = "made";
  this.seen = who(this);
}
const Bound = Made.bind({ label: "ignored" });
new Bound().seen;

Requirements

  1. answers has the keys a to h, each an object with a sloppy and a strict answer.
  2. Every answer is one of: "user", "timer", "first", "second", "made", "ignored", "globalThis", "undefined", "null", "number", "Number object".
  3. Each answer is what the snippet's last line really returns in that mode.

Examples

A snippet that is not one of the eight, and how its answer would be written:

JAVASCRIPT
const box = { label: "box", get() { return who(this); } };
const get = box.get;
get.call(null);
// sloppy: null becomes the global object → "globalThis"
// strict: this stays null → "null"
// written as: { sloppy: "globalThis", strict: "null" }
JAVASCRIPT
const tool = { label: "tool", run: function () { return (() => who(this))(); } };
tool.run();
// both modes: the arrow takes this from run, which was called on tool
// written as: { sloppy: "tool", strict: "tool" }

Environment

  • The tests read the global answers from predict.js, then run each snippet as its own classic script, once without and once with "use strict";, and compare the result with your answer.
  • Write the answers by reasoning, not by running the snippets: the point is to know the rules.

Hints

  1. For a regular function, look only at the call: what is to the left of the dot, if anything?
  2. Which kinds of functions do not care how they are called?
  3. Some code is strict even in a sloppy script. Which?

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…