JavaScript · Middle · Question 55 of 75
Why are event handlers often written as arrow-function class fields?
class Counter {
count = 0;
handleClick = () => { this.count++; };
}
button.addEventListener("click", counter.handleClick);- ✓Correct answer: Arrow functions capture
thisfrom the instance, so the method keeps working when passed as a callback - 2Class fields are faster than prototype methods
- 3Regular methods can't be used as event handlers
Explanation
A prototype method passed as a callback loses this. The trade-off: each instance gets its own copy of the function instead of sharing one on the prototype.