ByteTown
← ByteTown map

Chapter 5 · The Call Center

Who picks up depends on who's calling.

At The Call Center, a function doesn't know who it's talking to until the phone rings. this is decided by how the call is made, not where the function was written. Arrow functions never pick up at all.

The short answer

In a regular function, this is decided by how the function is called: new Fn() gives a fresh object, call/apply/bind give the object you pass, obj.fn() gives obj, and a plain fn() gives undefined in strict mode. Arrow functions have no this of their own and use the surrounding scope's.

Implicit vs default binding, and how a method loses its this.

Welcome to The Call Center. Inside a regular function, this is whoever placed the call, and it's decided fresh on every call. The dark pad on the right means 'nobody'.

Byte: Welcome to The Call Center. Inside a regular function, this is whoever placed the call, and it's decided fresh on every call. The dark pad on the right means 'nobody'.

Swipe here or tap ▶ to step through
🎯 0/2
  1. 1newnew Fn()→ a fresh object
  2. 2explicitcall / apply / bind→ the object you pass
  3. 3implicitobj.fn()→ left of the dot
  4. 4defaultfn()→ undefined (strict)
  5. arrow() => {}→ outer this
story-1.js
1const user = {
2 name: 'Ada',
3 hi() { console.log(this.name); },
4};
5const admin = { name: 'Grace', hi: user.hi };
6
7user.hi();
8admin.hi();
9const hi = user.hi;
10hi();
Console0 lines

Nothing logged yet…

  • Desk = an object that could be this
  • Dark pad = undefined: a plain call in strict mode
  • Gold cable = this, chosen by how the call was made
  • Blue cable = an arrow function borrowing the outer this
  • Red flash = this was undefined, so reading a property threw
  • Tip: use ← → to step, drag the scene to look around.

Your turn

Predict the output

Interviewers love these. Run the story in your head, pick an answer, and see why.

A borrowed method. (Assume strict mode / an ES module.)

1 / 6
1const counter = {
2 count: 5,
3 get() { return this.count; },
4};
5const get = counter.get;
6console.log(counter.get());
7console.log(get());

Before the interview

The cheat sheet

The whole chapter in a few lines, plus the follow-up questions people get caught on.

  • 1For regular functions, this is set by how the function is called, not where it's defined.
  • 2new binding: new Fn() creates a fresh object and makes it this.
  • 3Explicit binding: fn.call(obj, a, b), fn.apply(obj, [a, b]). fn.bind(obj) returns a new function with this fixed for good.
  • 4Implicit binding: obj.fn() makes this the object left of the dot.
  • 5Default binding: a plain fn() gets undefined in strict mode (modules, classes) or the global object (globalThis) in older non-strict scripts.
  • 6Precedence: new > bind/call/apply > obj.method() > plain call.
  • 7Arrow functions have no own this. They use the this of the surrounding scope, and call/apply/bind can't change it.
  • 8Detaching a method (const f = obj.m, passing obj.m as a callback) loses this. Fix with .bind(obj), an arrow wrapper, or a class field arrow.

Interview traps

›Why does setTimeout(obj.method, 0) lose this?

setTimeout receives just the function and later calls it without an object, so it's a plain call. Use setTimeout(() => obj.method()) or obj.method.bind(obj).

›Should object methods be arrow functions?

Usually not. An arrow method can't see the object as this, because it takes this from the outer scope. Use method shorthand, and use arrows for callbacks inside methods.

›call vs apply vs bind?

call and apply invoke immediately (arguments listed vs as an array). bind doesn't invoke; it returns a new function with this (and optionally some arguments) preset.

›What is this in an event listener?

With a regular function, it's the element the listener is attached to (event.currentTarget). With an arrow function, it's whatever this was outside.

Prefer reading?

The whole chapter, written out

Every step of every story, plus the practice questions with their answers.

The stories

›1. Who's calling?Implicit vs default binding, and how a method loses its this.
const user = {
  name: 'Ada',
  hi() { console.log(this.name); },
};
const admin = { name: 'Grace', hi: user.hi };

user.hi();
admin.hi();
const hi = user.hi;
hi();
  1. Welcome to The Call Center. Inside a regular function, this is whoever placed the call, and it's decided fresh on every call. The dark pad on the right means 'nobody'.
  2. line 1user is an object with a method, hi.
  3. line 5admin borrows the very same function. There's still only one hi.
  4. line 7Look left of the dot: user.hi() rings user's desk, so this = user and it logs Ada. This is implicit binding.
  5. line 8Same function, different caller: this = admin. Where hi was written doesn't matter, only how it's called.
  6. line 9hi copies the function out of the object. Nothing about the object comes with it.
  7. line 10A plain call has nothing left of the dot. In strict mode (on by default in modules and classes) this is undefined, so this.name throws. In older non-strict scripts it would be the global object (globalThis) instead.
  8. This is 'losing this'. Passing user.hi as a callback, like setTimeout(user.hi), ends in the same plain call. Fix it with bind or an arrow function.
›2. call, apply, bind & newChoosing this yourself, and the one rule that beats bind.
function intro(greeting) {
  console.log(greeting + ', ' + this.name);
}
const ada = { name: 'Ada' }, grace = { name: 'Grace' };

intro.call(ada, 'Hi');
intro.apply(grace, ['Hey']);
const adaIntro = intro.bind(ada);
adaIntro.call(grace, 'Yo');

function Caller(name) { this.name = name; }
const linus = new Caller('Linus');
console.log(linus.name);
  1. line 4One standalone function, intro, and two objects. intro doesn't belong to either.
  2. line 6call picks the desk directly: its first argument becomes this, and the rest are passed along. Explicit binding.
  3. line 7apply is the same, except the arguments come as an array.
  4. line 8bind doesn't call anything. It returns a new function with this welded to ada, permanently.
  5. line 9Trying to redirect it with call does nothing: a bound function always rings ada. Only new can override bind.
  6. line 12new builds a brand-new empty object, and that object is this inside the constructor. new beats every other rule.
  7. line 11this.name = name fills in the new object, and new returns it automatically.
  8. line 13linus.name is 'Linus'.
›3. Arrows don't pick upWhy a callback inside a method loses this, and how arrows fix it.
const team = {
  name: 'Byte crew',
  members: ['Ada', 'Grace'],
  list() {
    this.members.forEach(function (m) {
      console.log(m, this?.name);
    });
    this.members.forEach((m) => {
      console.log(m, this.name);
    });
  },
};
team.list();
  1. line 1team has a name, a list of members, and a method that loops over them twice.
  2. line 13team.list() is a method call, so inside list, this = team.
  3. line 6forEach calls the regular callback as a plain function: no dot, no caller. Its this is undefined, so this?.name is undefined.
  4. line 6Same for Grace. The callback answers its own phone, and nobody's calling.
  5. line 9Arrow functions don't have their own this at all. They use the this of the place they were written: list's this, which is team.
  6. line 9So both members get the crew's name. That's why arrows are the go-to for callbacks inside methods.
  7. Precedence, highest first: new, then call/apply/bind, then obj.method(), then a plain call. Arrows skip the list and inherit this from outside.

Practice questions, answered

›A borrowed method. (Assume strict mode / an ES module.)
const counter = {
  count: 5,
  get() { return this.count; },
};
const get = counter.get;
console.log(counter.get());
console.log(get());

Answer: 5, then TypeError

counter.get() binds this to counter. get() is a plain call, so this is undefined in strict mode and reading .count throws a TypeError.

›An arrow function as an object method (in an ES module).
const pet = {
  name: 'Rex',
  speak: () => console.log(this?.name),
};
pet.speak();

Answer: undefined

Arrows ignore how they're called and use the outer this. At the top level of a module, this is undefined, so this?.name is undefined.

›Can call override bind?
function who() { return this.name; }
const a = { name: 'A' }, b = { name: 'B' };
const bound = who.bind(a);
console.log(bound.call(b));

Answer: A

A bound function's this is fixed. call, apply and even another bind can't change it. Only new can.

›What does new do to this?
'use strict';
function Pizza(top) {
  this.top = top;
}
const p = new Pizza('basil');
console.log(p.top);
Pizza('x');

Answer: basil, then TypeError

new creates a fresh object as this. Without new, Pizza('x') is a plain call. In strict mode this is undefined, so this.top = ... throws a TypeError.

›The nested function trap.
const shop = {
  name: 'Pizza Co',
  open() {
    function inner() { return this?.name; }
    const arrow = () => this.name;
    return [inner(), arrow()];
  },
};
console.log(shop.open());

Answer: [undefined, 'Pizza Co']

inner() is a plain call, so its this is undefined. The arrow uses open's this, which is shop.

›Class methods passed as callbacks.
class Btn {
  label = 'OK';
  click() { return this.label; }
  clickArrow = () => this.label;
}
const b = new Btn();
const { click, clickArrow } = b;
console.log(clickArrow());
console.log(click());

Answer: OK, then TypeError

A class field arrow captures the instance as this when it's created, so clickArrow still works detached. click is a regular method: called plainly (class bodies are strict), this is undefined and .label throws.

Finished the story?

Stamp it on your map. Progress is saved in this browser.

Back to the map

Coming soon · Practice Mode

Want drills and mock interviews for this?

Join the waitlist and get early access when it opens.

No spam. One email when it launches, and you can unsubscribe anytime.