The `this` Keyword, `call`, `apply`, and `bind`

Intermediate · 34 min

Why `this` is decided at call time, not definition time, the binding rules behind it, and how `call`, `apply`, and `bind` override it on purpose.

Before thisFunction Declarations, Expressions, and Arrow Functions

Gordon Zhu · 34 min

The `this` Keyword, `call`, `apply`, and `bind`

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Gordon ZhuReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `this` is almost never determined by where a regular function is written — it's determined by how it's *called*. The same function can produce a different `this` on every single call, depending on what sits to the left of the dot when it's invoked.
  • Four situations cover most of it: a plain function call gives you `this === undefined` in strict mode (the global object otherwise); calling a function as `obj.method()` binds `this` to `obj`; calling with `new` binds `this` to the newly created object; and an arrow function ignores all of that and just uses the `this` from its enclosing scope.
  • `call` and `apply` invoke a function immediately with `this` set explicitly to whatever you pass as the first argument — they differ only in how they take the remaining arguments: `call` takes them one by one, `apply` takes them as an array.
  • `bind` doesn't call the function at all — it returns a *new* function with `this` permanently locked to whatever you passed, ready to be called (or handed off as a callback) later.
  • The classic "losing `this`" bug: passing `obj.method` as a callback (to `setTimeout`, an event listener, `.map()`) detaches the function from `obj` — by the time it's called, there's no `obj.` on the left of the call, so `this` isn't what you expected. `bind`, an arrow function wrapper, or capturing `this` in a variable are the three standard fixes.

Why this needs rules at all

In most languages, a method’s self or this is simple: it’s the object the method is attached to, full stop. JavaScript makes a different, more flexible choice — this is just another variable that gets a value at call time, decided by how the function was invoked, not where it was defined. That flexibility is what makes call, apply, and bind possible: you can run the same function with a different this on purpose. It’s also what causes a huge share of real-world JavaScript bugs, because “how it’s called” is easy to change by accident — passing a method somewhere else as a plain value is enough to break it.

The binding rules, in the order they get checked

Plain call. greet() called with nothing in front of it gets this === undefined in strict mode (or the global object in loose, legacy mode). No object owns this call, so there’s nothing to bind this to.

Implicit binding. user.greet() binds this to user — whatever sits immediately to the left of the dot at the call site becomes this inside the function. This is the rule behind “normal” method calls, and it’s also the rule that silently stops applying the moment you detach the method (const g = user.greet; g(); — no object to the left of the call anymore, so this rule doesn’t fire).

new binding. new Thing() creates a brand-new object and binds this to it inside the constructor function, regardless of how Thing would otherwise have bound this.

Explicit binding. call, apply, and bind let you state this directly, overriding whatever the call site would have produced on its own — covered in detail below.

Arrow functions opt out of all of it. An arrow function never gets its own this to bind, under any of the four rules above. It just reads this from the scope it was written inside, permanently. This is the detail the previous lesson flagged and deferred to here.

The video for this lesson walks through every one of these — plain calls, method calls, callbacks, constructors, arrow functions, and the explicit tools below — with the same running example, which is the clearest way to see that it’s one consistent system, not five unrelated special cases.

call and apply: run it now, with this this

function introduce(greeting) {
  return `${greeting}, I'm ${this.name}`;
}

const user = { name: "Ada" };

introduce.call(user, "Hi");     // "Hi, I'm Ada"
introduce.apply(user, ["Hi"]);  // "Hi, I'm Ada"

Both invoke introduce immediately, with this forced to user instead of whatever the call site would have produced. The only difference is how the remaining arguments show up: call takes them one at a time, apply takes them as a single array — useful when you already have the arguments sitting in an array and don’t want to spread them apart yourself (spread syntax has mostly replaced this specific use of apply, but you’ll still see it in older code).

bind: lock in this, call it whenever

const greetAda = introduce.bind(user);
greetAda("Hey"); // "Hey, I'm Ada" — this is always `user`, even called bare

bind doesn’t run introduce at all — it returns a new function with this permanently fixed to user, no matter how that new function later gets called. That permanence is exactly what you want when a function is about to be handed somewhere else to be invoked later and you won’t get a say in how.

The bug this all leads to: losing this

class Timer {
  constructor() { this.seconds = 0; }
  tick() { this.seconds++; }
}

const t = new Timer();
setTimeout(t.tick, 1000); // this.seconds++ fails — `this` isn't `t` anymore

t.tick looks like it’s still attached to t, but passing it as a bare reference strips that away — setTimeout calls it as a plain function, not as t.tick(), so the implicit-binding rule never fires and this isn’t t. This is the single most common this bug in real code: any time a method gets handed off as a callback — to setTimeout, an event listener, .then(), .map() — it’s at risk of losing its this the same way. The fixes all boil down to restoring the connection explicitly: setTimeout(t.tick.bind(t), 1000), or setTimeout(() => t.tick(), 1000), since the arrow function captures this from the surrounding scope instead of from how it gets called.

Where this fits

This is the payoff lesson for everything the “Functions and this” group has been building toward — arrow functions’ lexical this only makes sense once you’ve seen the rules a regular function follows instead. It also sets up ES6 classes and inheritance, where methods on a class instance run into this exact this-binding behavior constantly, usually around event handlers and callbacks.

Check yourself

  1. 01If a method is defined once on an object, why can `this` inside it refer to something different each time it's called?

    Because `this` isn't tied to where the function was defined — it's set fresh by the call itself, based on how the function is invoked. Call it as `obj.method()` and `this` is `obj`; copy the same function reference into a variable and call it plain, and `this` is no longer `obj` at all.

  2. 02What's the actual difference between `fn.call(ctx, 1, 2)` and `fn.apply(ctx, [1, 2])`?

    They do the same thing — invoke `fn` immediately with `this` set to `ctx` — and differ only in argument shape: `call` takes the rest of the arguments individually, comma-separated, while `apply` takes them bundled into a single array.

  3. 03Why does `bind` return a function instead of just running it?

    Because `bind` is for cases where you need the `this`-locked version *later* — as a callback passed to something else, or stored and called repeatedly — not right now. `call` and `apply` run the function immediately with the given `this`; `bind` hands out a version of the function for someone else to run later, without letting them change what `this` is.

  4. 04A button's `onClick` handler is set to `widget.handleClick`, and inside that method `this` turns out to be `undefined` instead of `widget`. What happened, and what are two ways to fix it?

    The method reference was detached from `widget` the moment it was passed as a bare value — the click handler calls it as a plain function, not as `widget.handleClick()`, so none of the implicit-binding rule applies. Fixing it means restoring that link explicitly: pass `widget.handleClick.bind(widget)` instead, or wrap the call in an arrow function (`() => widget.handleClick()`), which captures `this` from the surrounding scope rather than from how the handler gets invoked.

  5. 05Why does an arrow function ignore every one of the binding rules that apply to a regular function?

    Because an arrow function was never designed to have its own `this` in the first place — it has no `this` binding of its own to be set by `call`, implicit binding, or `new`. It always reads `this` from the nearest enclosing non-arrow scope, fixed at the point it's written, which is exactly what makes it immune to the losing-`this` bug that regular functions are prone to.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark