微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么在声明前引用常量不抛出?

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。