LearnFrenzy
Sign in Take a free test

Callbacks · Callbacks MCQ · Question 10

this inside a callback Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

Inside a method, why does setTimeout(function () { this.count++; }, 0) not update the object's count?

  • A setTimeout freezes objects
  • B count is private
  • C Timers cannot change numbers
  • D The regular callback gets its own this from how the timer calls it, not the method's this – use an arrow function Correct
Correct Answer: Option D

this in a regular function depends on the caller (the global object in browsers, a Timeout object in Node.js). An arrow keeps the method's this.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links