Skip to content

How to Fix JavaScript 'this' Context Lost

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix JavaScript 'this' Context Lost. We cover key concepts, practical examples, and best practices.

The Problem

JavaScript loses the this context when a method is passed as a callback or extracted from its object, causing this to become undefined in strict mode or the global object in non-strict mode.

Quick Fix

Step 1: Use an arrow function

Arrow functions inherit this from the enclosing scope:

const user = {
    name: 'Alice',
    greet: function() {
        console.log('Hello, ' + this.name);
    }
};
setTimeout(user.greet, 100);
Hello, undefined

Wrap the call in an arrow function:

const user = {
    name: 'Alice',
    greet: function() {
        console.log('Hello, ' + this.name);
    }
};
setTimeout(() => user.greet(), 100);
Hello, Alice

Step 2: Use .bind()

Explicitly bind the context:

const user = {
    name: 'Alice',
    greet: function() {
        console.log('Hello, ' + this.name);
    }
};
setTimeout(user.greet.bind(user), 100);
Hello, Alice

Step 3: Use a class property arrow function

In class components, define methods as arrow functions:

class User {
    constructor(name) {
        this.name = name;
    }
    greet = () => {
        console.log('Hello, ' + this.name);
    }
}
const user = new User('Alice');
setTimeout(user.greet, 100);
Hello, Alice

Step 4: Store this in a variable

For older JavaScript, capture this in a variable:

const user = {
    name: 'Alice',
    greet: function() {
        const self = this;
        setTimeout(function() {
            console.log('Hello, ' + self.name);
        }, 100);
    }
};
user.greet();
Hello, Alice

Prevention

  • Prefer arrow functions for callbacks and event handlers.
  • Use .bind() when passing object methods as callbacks.
  • Define React class component methods as arrow functions.
  • Avoid extracting methods from objects without binding them.

Common Mistakes with this context

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does this become undefined in strict mode?

In strict mode, functions called without an explicit context receive undefined as this instead of the global object. This was changed in ES5.1 to prevent accidental global leaks. When you pass user.greet as a callback, the function loses its receiver, so this becomes undefined.

How does the bind method work internally?

Function.prototype.bind() creates a new function with a fixed this value. It does not call the original function. Internally, it wraps the function with a call to .call() or .apply() using the bound context. The bound function also supports partial application of arguments.

Can I use call or apply instead of bind?

Yes, but call and apply invoke the function immediately, which is useful for one-off context changes. Use call when you need to invoke once with a specific context. Use bind when you need to create a reusable callback with a fixed context. For example: handler.call(context, arg) invokes immediately; handler.bind(context) creates a bound copy.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro