7.6. Predict this in eight calls
Topics
This question is in
7. this, call, apply and bindMost 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:
const answers = {
a: { sloppy: "?", strict: "?" },
// … b to h
};Every snippet uses this helper, which turns this into a word:
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.
const user = { label: "user", show() { return who(this); } };
user.show();b.
const user = { label: "user", show() { return who(this); } };
const show = user.show;
show();c.
const user = { label: "user", show: () => who(this) };
user.show();d.
const timer = {
label: "timer",
start() { return [1].map(() => who(this))[0]; },
};
timer.start();e.
function kind() { return who(this); }
kind.call(7);f.
class Counter {
show() { return who(this); }
}
const show = new Counter().show;
show();g.
const user = { label: "user", show() { return who(this); } };
const bound = user.show.bind({ label: "first" });
bound.call({ label: "second" });h.
function Made() {
this.label = "made";
this.seen = who(this);
}
const Bound = Made.bind({ label: "ignored" });
new Bound().seen;Requirements
answershas the keysatoh, each an object with asloppyand astrictanswer.- Every answer is one of:
"user","timer","first","second","made","ignored","globalThis","undefined","null","number","Number object". - 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:
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" }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
answersfrompredict.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
- For a regular function, look only at the call: what is to the left of the dot, if anything?
- Which kinds of functions do not care how they are called?
- 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