问题描述
为什么以下代码不会在第(*)
行抛出?
{
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();
}
解决方法
在设置变量后调用该函数。
变量Tracker
设置在之间
//console.log(Tracker); // Throws
和
Tracker.attached();
行
update.call(Tracker);
没有被叫过
Tracker.attached();
按照调试器中的程序流程进行操作,或添加一些console.log
调试以进行查看。