Explain the concept of `this` binding in event handlers
TL;DR
When a regular function is registered with addEventListener, the browser calls it with this set to the element the listener is attached to, i.e. event.currentTarget (not necessarily event.target, the element that was actually clicked). Arrow functions ignore that and keep the this of their surrounding code, which is what you want inside class or component methods. When you pass a method, bind it (and keep the bound reference so you can remove it) or use the handleEvent object pattern.
Regular function vs arrow
<ul id="menu">
<li><button>Save</button></li>
</ul>
menu.addEventListener('click', function (event) {
console.log(this === menu); // true: this === event.currentTarget
console.log(event.target.tagName); // 'BUTTON': where the click happened
});
menu.addEventListener('click', (event) => {
console.log(this); // outer this (undefined in a module)
console.log(event.currentTarget === menu); // true: use this instead
});
Prefer event.currentTarget in all styles. It's explicit and doesn't depend on how the function was written. Note it's only set during dispatch; it becomes null afterwards, so read it synchronously (not after an await).
Methods as handlers
class Dropdown {
open = false;
constructor(button) {
this.button = button;
// ❌ button.addEventListener('click', this.toggle) → `this` would be the button
this.toggle = this.toggle.bind(this); // bind once, keep the reference
button.addEventListener('click', this.toggle);
}
toggle() { this.open = !this.open; }
destroy() { this.button.removeEventListener('click', this.toggle); }
}
Alternatives:
class Dropdown2 {
open = false;
toggle = () => { this.open = !this.open; }; // class-field arrow: bound per instance
}
// Or remove everything with one signal
const ac = new AbortController();
button.addEventListener('click', () => dropdown.toggle(), { signal: ac.signal });
ac.abort();
The handleEvent pattern
Any object with a handleEvent method can be a listener, and this is the object itself:
const tracker = {
clicks: 0,
handleEvent(e) {
if (e.type === 'click') this.clicks++;
},
};
document.addEventListener('click', tracker);
document.removeEventListener('click', tracker); // same reference, easy removal
This avoids bind entirely and lets one object handle several event types.
Inline handlers (legacy)
<button onclick="console.log(this)">Go</button> <!-- this is the button -->
<button onclick="handle()">Go</button> <!-- inside handle(), this is undefined/window -->
Inline attributes run with the element as this only in the attribute's own code, not in functions it calls. They also require unsafe-inline in a Content Security Policy, so avoid them.
Frameworks
- React: handlers are called without a meaningful
this. Class components needed binding or class-field arrows; function components don't usethisat all. Reade.currentTargetfrom the synthetic event. - Vue/Angular: templates call methods on the component instance, so
thisis the component.
Pitfalls
- Using
thisinside a delegated handler expecting the clicked child. It's the container; useevent.target.closest(selector). removeEventListener(type, this.method.bind(this))doesn't work: new function each time.- Mixing arrows and
thisexpecting the element.
In an interview, mention…
- Regular listeners get
this === event.currentTarget; arrows keep the outerthis. currentTargetvstarget, especially with delegation.- Binding strategies (bind once, class-field arrows,
handleEvent,AbortSignal) and the removal problem.
References
Spotted a mistake or a rights concern? Report an issue