# this in Classes & Method Binding

> Master the most confusing keyword in JavaScript. Learn why 'this' gets lost in callbacks and how to lock it down using Arrow Functions.

Subject: JavaScript | Lesson 25 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/this-in-classes
Previous: [Private Fields, Getters & Static](https://codewithanimation.in/javascript/private-fields-and-static.md) | Next: [Arrays: Create, Mutate & Access](https://codewithanimation.in/javascript/arrays-fundamentals.md)

## The fragile 'this' keyword

In JavaScript classes, the `this` keyword refers to the current instance of the object. For example, `this.name` gets the name of the specific object executing the method.

However, `this` is not locked in place. Its value is determined entirely by **how the method is called**, not where it was defined.

## Losing Context in Callbacks

If you call a method directly like `player.attack()`, `this` works perfectly and points to `player`.

But if you pass that method as a callback to an event listener or a timeout (e.g., `setTimeout(player.attack, 1000)`), it gets separated from the object! When the timer finally runs the function, it calls it nakedly as `attack()`. Because classes run in strict mode, `this` instantly becomes `undefined`.

## The Modern Fix: Arrow Functions

Historically, developers fixed this by manually binding the method in the constructor using `.bind(this)`. This forced the method to permanently remember its context.

Today, the modern solution is to use **Class Fields with Arrow Functions**. If you define a method as `attack = () => { console.log(this.name) }`, the arrow function automatically locks `this` to the instance at the moment of creation. It can never be lost!

## Common mistake: React Event Handlers

If you ever write class-based React components, losing `this` in event handlers like `onClick={this.handleClick}` is the most common cause of crashes. Using arrow function class fields completely solves this.

## Tip: Memory Tradeoffs

Standard methods are attached to the Class Prototype, meaning 1,000 instances share 1 method in memory. Arrow function fields, however, are attached to the *instances* directly. If you create 1,000 instances, you create 1,000 copies of the function! Usually, the memory cost is negligible, but it is worth knowing.

## Try it: The Binding Sandbox

Compare a standard class method against an arrow function field to see exactly when and why 'this' gets ripped away.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/this-in-classes

## Check yourself

### Question 1

Why does `setTimeout(player.attack, 1000)` cause `this` to become undefined?

- A. Because setTimeout runs in the global scope, stripping the method from its object.
- B. Because setTimeout does not support class methods.
- C. Because the player object gets garbage collected.

Correct answer: A

When you pass `player.attack`, you are just passing the raw function. When `setTimeout` eventually calls it, it calls it as a standalone function, losing the `player` context.

- Why not B: Incorrect. It supports them, but you lose the execution context.
- Why not C: Incorrect. The object still exists, but the function lost its pointer to it.

### Question 2

How does an Arrow Function fix the 'this' problem?

- A. Arrow functions lexically bind 'this', locking it to the instance where they were created.
- B. Arrow functions automatically compile to use .bind() under the hood.
- C. Arrow functions cannot use 'this', which prevents errors.

Correct answer: A

Arrow functions don't have their own `this`. They inherit `this` from the surrounding scope exactly as it was when the function was created.

- Why not B: Incorrect. It's a fundamental feature of how arrow functions resolve context, not a compiler trick.
- Why not C: Incorrect. They absolutely can and do use `this`.

### Question 3

Which approach uses less memory if you instantiate 10,000 objects?

- A. Standard methods (e.g., attack() { ... })
- B. Arrow function fields (e.g., attack = () => { ... })
- C. Both use the exact same amount of memory.

Correct answer: A

Standard methods live on the Prototype, meaning all 10,000 objects share a single function in memory. Arrow fields attach to the instances themselves, creating 10,000 copies.

- Why not B: Incorrect. Arrow fields create a unique function in memory for every single instance.
- Why not C: Incorrect.

## Remember this

- In JavaScript, the value of `this` is determined by HOW a function is called, not where it is defined.
- Passing a standard class method as a callback strips away its object context, making `this` undefined.
- Arrow Functions lexically bind `this`, meaning it permanently locks onto the surrounding instance.
- Using class fields with arrow functions (`myFunc = () => {}`) is the modern fix for lost context.
- Arrow functions cost slightly more memory because they exist on the instance, not the Prototype.
