$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #91

Which fix removes the bug?

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