Which fix removes the bug?
class Counter {
constructor() { this.n = 0; }
tick() {
[1, 2].forEach(function () { this.n++; });
}
}
- Use an arrow function callback
- Rename
this.ntoself.n - Move
tickinto the constructor - Declare
nas a static field
A plain function gets its own
Play Problem Solving ▸
this; an arrow keeps the enclosing one.