$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #138

Which fix removes the bug?

class Counter {
  constructor() { this.n = 0; }
  tick() {
    [1, 2].forEach(function () { this.n++; });
  }
}
  1. Use an arrow function callback
  2. Rename this.n to self.n
  3. Move tick into the constructor
  4. Declare n as a static field
A plain function gets its own this; an arrow keeps the enclosing one.
Play Problem Solving ▸