Why the following code does not throw at line (*)
?
{
function update() {
console.log(this);
}
const THROTTLE_TIMEOUT = 1000;
let throttled = false;
function stopThrottling() {
throttled = false;
}
function mouseMoveHandler(e) {
if (throttled) return; throttled = true;
setTimeout(stopThrottling, THROTTLE_TIMEOUT);
update.call(Tracker); // (*)
}
//console.log(Tracker); // Throws
const Tracker = {
attached() {
window.addEventListener('mousemove', mouseMoveHandler);
}
}
Tracker.attached();
}
The function is called after the variable is set.
The variable
Tracker
is set betweenand
The line
isn't called before
Follow the program flow in your debugger or add some
console.log
debugging to see it.