01 / 21
this

this Binding
in Classes

Master how JavaScript's this keyword behaves inside class methods: when it works, when it breaks, and how to fix it.

JavaScript

What is this?

this is a special keyword in JavaScript that refers to the current execution context: the object that is "owning" or calling the function at the moment it runs.

In classes, this usually refers to the instance of the class: the specific object you created with new.

A Quick Class Refresher

class Dog { constructor(name) { this.name = name; } bark() { return this.name + " says woof!"; } } const rex = new Dog("Rex"); console.log(rex.bark()); // "Rex says woof!"

The Dot Rule of this

object rex . method bark() this = rex rex.bark() Whatever is left of the dot becomes "this"

The Core Rule for Classes

1
In the constructor: this refers to the brand-new object being created by new.
2
In a method called via dot notation: this refers to the object left of the dot.
3
In a detached method: this becomes undefined (strict mode) or the global object. This is the main trap.

When this Breaks

class Dog { constructor(name) { this.name = name; } bark() { return this.name + " says woof!"; } } const rex = new Dog("Rex"); const detached = rex.bark; // No parentheses! Just a reference. console.log(detached()); // No dot, no object! // TypeError: Cannot read properties of undefined

Visualizing the Detachment

✓ Works rex.bark() this = rex dot provides context ✕ Breaks detached() this = undefined no dot, no context Common detachment scenarios: setTimeout callback addEventListener event handler const fn = obj.m variable assignment All of these detach the method from its object

Regular vs Arrow Functions

Regular Method Arrow Function
Syntax bark() { ... } bark = () => { ... }
this binding Set at call time (dynamic) Set at definition time (lexical)
Can be detached? Breaks if detached Safe when detached
Where it lives On the prototype (shared) On each instance (copied)
Memory Efficient (one copy) Less efficient (per instance)

Three Ways to Fix this

1. Arrow Field
Define the method as an arrow function class field. It captures this at creation.
2. .bind()
In the constructor, bind the method: this.bark = this.bark.bind(this).
3. Wrapper Arrow
Wrap the call in an arrow at the call site: () => rex.bark().

Step-by-Step Checklist

1
Spot the method call. Find where the class method is being invoked.
2
Check for a dot. Is it called as object.method()? If yes, this is the object.
3
Check for detachment. Is the method passed as a callback, stored in a variable, or used in setTimeout?
4
If detached, apply a fix: use an arrow field, .bind(this), or a wrapper arrow function.
5
Verify: mentally trace what this will be at the exact moment the function executes.

Example 1: Arrow Field Fix

class Timer { constructor() { this.seconds = 0; } // Arrow field: this is always the Timer instance tick = () => { this.seconds++; console.log(this.seconds); } } const t = new Timer(); setInterval(t.tick, 1000); // 1, 2, 3, 4 ... ✓ Works perfectly!

Example 2: The .bind() Fix

class Button { constructor(label) { this.label = label; // Lock this permanently in the constructor this.handleClick = this.handleClick.bind(this); } handleClick() { console.log(this.label + " clicked"); } } const btn = new Button("Save"); document.addEventListener("click", btn.handleClick); // "Save clicked" ✓ — this is still btn

Example 3: Wrapper Arrow at Call Site

class Player { constructor(name) { this.name = name; } greet() { return "Hi, I'm " + this.name; } } const p = new Player("Ada"); // Wrap in an arrow so the dot is preserved inside: setTimeout(() => console.log(p.greet()), 1000); // "Hi, I'm Ada" ✓ — the dot call happens inside the arrow

The this Decision Flowchart

Method is called Called with dot? obj.method() Yes this = obj ✓ No Arrow or bound? Yes this = captured ✓ this = undefined ✕ Apply a fix: arrow, bind, or wrapper

Common Mistakes

✕ Passing method as callback
Writing setTimeout(obj.tick, 1000) detaches tick from obj. The dot context is lost before the timer fires.
✕ Forgetting .bind() returns new fn
Calling this.fn.bind(this) without assigning it back does nothing. You must store the result.
✕ Using arrow for prototype method
Arrow fields create a new copy per instance. Overusing them wastes memory when you have many objects.

Summary Table

Call Pattern this = Safe?
obj.method() obj ✓ Yes
const fn = obj.method; fn() undefined ✕ No
setTimeout(obj.method, 100) undefined ✕ No
method = () => { ... } (arrow) instance (captured) ✓ Always
this.method.bind(this) instance (locked) ✓ Always
() => obj.method() (wrapper) obj (dot preserved) ✓ Always

Question 1 of 5

What happens when inc() is called?

Question 2 of 5

Which fix allows a class method to keep its this context when passed as a callback?

Question 3 of 5

What does setTimeout print after 100ms?

Question 4 of 5

What does .bind(this) in a constructor actually do?

Question 5 of 5

What happens when process() runs?