JavaScript · Middle · Question 4 of 75
What does this code print after a second?
const timer = {
seconds: 10,
start() {
setTimeout(function () { console.log(this.seconds); }, 1000);
},
};
timer.start();- ✓Correct answer:
undefined: a regular callback function has its ownthis - 2
10: the callback getsthisfrom thestartmethod - 3
ReferenceError:thisis unavailable inside a timer
Explanation
The timer calls the callback without a context, so this isn't timer. An arrow function () => console.log(this.seconds) would take this from start and print 10. This is the classic case where arrows are useful.