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

Which fix removes the bug?

class Timer {
  constructor() { this.ticks = 0; }
  start() { setInterval(function () { this.ticks++; }, 1000); }
}
  1. Use an arrow function
  2. Store this in self
  3. Bind inside the constructor
  4. Call start with apply
A plain function gets its own this; an arrow keeps the enclosing one.
Play Problem Solving ▸