27/361

What are the common pitfalls of using the `this` keyword?

Quiz · JavaScript
Easy
5 min
this
functions

TL;DR

Almost every this bug comes from one fact: for regular functions, this is set by how a function is called, not where it's defined. The common traps are detaching a method (passing obj.method as a callback), nested regular functions inside methods, using arrow functions as object methods, and confusion around event handlers, classes and module top level. The fixes are arrows for inner callbacks, bind or class-field arrows for passed methods, and strict mode so mistakes throw instead of silently hitting the global object.

1. Detached methods

class Player {
  volume = 5;
  louder() { this.volume++; }
}
const p = new Player();

setTimeout(p.louder, 0);              // TypeError: this is undefined (class code is strict)
button.addEventListener('click', p.louder); // `this` is the button, not p
const { louder } = p; louder();       // undefined again

Fixes:

setTimeout(() => p.louder(), 0);      // call it as a method
button.addEventListener('click', p.louder.bind(p));
class Player2 { volume = 5; louder = () => { this.volume++; }; } // per-instance bound

2. Nested regular functions

const gallery = {
  images: ['a.png', 'b.png'],
  prefix: '/img/',
  urls() {
    return this.images.map(function (img) {
      return this.prefix + img; // `this` is undefined here
    });
  },
};

Fix: use an arrow function (img => this.prefix + img), which inherits this from urls. The old const self = this workaround is no longer needed.

3. Arrow functions as object methods

const cart = {
  items: [],
  add: (item) => this.items.push(item), // `this` is NOT cart
};

Arrows take this from the surrounding scope, which here is the module (undefined) or global. Use method shorthand: add(item) { this.items.push(item); }.

4. Expecting this to be the element in arrow listeners

el.addEventListener('click', () => this.classList.toggle('on')); // wrong `this`
el.addEventListener('click', (e) => e.currentTarget.classList.toggle('on')); // explicit and clear

Prefer event.currentTarget over this in handlers. It works regardless of function style.

5. Sloppy mode silently using the global object

function setName(n) { this.name = n; }
setName('x'); // sloppy: overwrites window.name (a special built-in property!); strict: TypeError

ES modules and classes are strict by default, so this mostly bites in legacy scripts.

6. Re-binding a bound or arrow function

const f = function () { return this.id; }.bind({ id: 1 });
f.call({ id: 2 }); // 1: bound functions ignore later call/apply/bind

Libraries that call your callback with a specific this (old jQuery handlers, some test frameworks such as Mocha's this.timeout()) won't work if you pass an arrow function.

7. Bound functions and listener removal

el.addEventListener('scroll', this.onScroll.bind(this));
el.removeEventListener('scroll', this.onScroll.bind(this)); // no effect: different function

Store the bound function once, or use an AbortController signal to remove listeners.

8. this at the top level differs by environment

undefined in ES modules, window in classic scripts, module.exports in CommonJS. Code that relies on top-level this breaks when moved between them. Use globalThis when you mean the global object.

In an interview, mention…

  • The root cause: call-site binding for regular functions.
  • A go-to set of fixes: arrows for inner callbacks, method shorthand for object methods, bind/class-field arrows when passing methods, event.currentTarget in handlers.
  • Strict mode turning silent global writes into errors, and the bound-listener removal trap.

Spotted a mistake or a rights concern? Report an issue