【问题标题】:alert() sometimes causes an error in Chromealert() 有时会导致 Chrome 出错
【发布时间】:2015-08-07 07:11:00
【问题描述】:

我曾经使用alert() 进行调试,当我在 JavaScript/jQuery 中编写一些代码行时(我知道,这是不好的风格)。我现在在 Google Chrome 中看到一个奇怪的行为。

HTML

    <html>
    <head>
        <script src="my_script.js"></script>
    </head>
    <body>
    <p>Test</p>
    </body>
    </html>

my_script.js

这真的很简单。

    alert("hello world");

错误

当我加载页面时它通常可以正常工作,但是当我重新加载页面时有时谷歌浏览器会出现以下错误:

Uncaught TypeError: Cannot read property 'impl' of undefined extensions::messaging:184

经过多次测试,我发现

  • alert() 窗口按预期显示
  • 错误并非每次都会发生,但有时会发生
  • 错误发生(发生时)关闭alert()窗口后
  • 如果我等待关闭 alert() 窗口,或在两次重新加载之间,似乎不会发生错误

错误信息指向这个代码-sn-p:

  // In case the extension never invokes the responseCallback, and also
  // doesn't keep a reference to it, we need to clean up the port. Do
  // so by attaching to the garbage collection of the responseCallback
  // using some native hackery.
  messagingNatives.BindToGC(responseCallback, function() {
    if (port) {
      privates(port).impl.destroy_();
      port = null;
    }
  });

但我什至无法弄清楚哪个文件受到影响以及它被放置在哪个文件夹中。

Google 刚刚发现了另一个明显相同错误的描述: https://teamtreehouse.com/forum/anyone-else-getting-error-cannot-read-property-impl-of-undefined

这个页面的作者假设这个错误只是有时发生的事实,指向一个异步进程的问题,有时没有完成重新加载页面。

我通过改用console.log() 解决了我的调试任务。但我很感兴趣究竟发生了什么错误,以及如何通过使用alert() 函数来避免它返回到我糟糕的调试方式。

【问题讨论】:

  • 听起来像一个 chrome 错误。举报。

标签: javascript google-chrome


【解决方案1】:

嘿(:我在这个论坛(https://teamtreehouse.com/forum/anyone-else-getting-error-cannot-read-property-impl-of-undefined)中找到了您问题的可能解释。我引用:'在访问属性 impl 之前返回未定义。由于它是间歇性发生的,这向我表明 privates( port) 是一个异步操作 - 它允许其他代码继续执行 - 有时 privates 会在访问属性 impl 之前完成,有时它不会。'结束引用。尽管这只是一个推测,但我觉得它是合理的

【讨论】:

  • 非常感谢,但我已经找到了该页面(请参阅我的问题),它没有回答如何避免错误。
  • 如果不是在单独的 javascript 文件中生成警报,而是在身体负荷上定义它,会怎样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2021-12-23
  • 1970-01-01
  • 2014-03-23
  • 2022-12-24
  • 2018-08-24
相关资源
最近更新 更多