What is the correct fix?
const obj = {
count: 0,
start() {
setTimeout(function () {
this.count++;
}, 10);
}
};
- Use an arrow callback so
thisis kept - Use a normal callback so
thisis kept - Change
this.count++tocount++there - Move
setTimeoutoutside of the method
A normal function gets its own
Play Problem Solving ▸
this; an arrow keeps the enclosing method's this.