Which fix removes the bug?
class Timer {
constructor() { this.ticks = 0; }
start() { setInterval(function () { this.ticks++; }, 1000); }
}
- Use an arrow function
- Store
thisinself - Bind inside the constructor
- Call start with apply
A plain function gets its own
Play Problem Solving ▸
this; an arrow keeps the enclosing one.