为什么在声明前引用常量不抛出?

问题描述

为什么以下代码不会在第(*)行抛出?

{
  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调试以进行查看。