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

What is the correct fix?

const obj = {
  count: 0,
  start() {
    setTimeout(function () {
      this.count++;
    }, 10);
  }
};
  1. Use an arrow callback so this is kept
  2. Use a normal callback so this is kept
  3. Change this.count++ to count++ there
  4. Move setTimeout outside of the method
A normal function gets its own this; an arrow keeps the enclosing method's this.
Play Problem Solving ▸