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.
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
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.
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); }.
this to be the element in arrow listenersel.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.
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.
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.
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.
this at the top level differs by environmentundefined 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.
bind/class-field arrows when passing methods, event.currentTarget in handlers.Spotted a mistake or a rights concern? Report an issue