【发布时间】:2021-01-15 11:37:39
【问题描述】:
为什么下面的代码不会在(*) 行抛出?
{
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();
}
【问题讨论】:
-
设置变量后调用该函数。在调试器中启动代码,您将看到它。
标签: javascript hoisting