Which fix removes the bug?
class Counter {
n = 0;
inc() { this.n++; }
}
btn.addEventListener('click', new Counter().inc);
- Wrap it:
() => counter.inc()as handler - Wrap it:
() => Counter.inc()as handler - Change
inc()to a static class method - Change
n = 0tothis.n = 0in class
Passing the method loses its receiver, so
Play Problem Solving ▸
this is undefined when it runs.