当我在 JavaScript/jQuery 中编写一些代码时,我曾经使用 alert()
进行调试(我知道,这是糟糕的风格)。我现在在 Google Chrome 中看到一个奇怪的行为。
HTML
<html>
<head>
<script src="my_script.js"></script>
</head>
<body>
<p>Test</p>
</body>
</html>
my_script.js
这真的很简单。
alert("hello world");
错误
当我加载页面时它通常工作正常,但是当我重新加载页面时有时 Google Chrome 中会出现以下错误:
Uncaught TypeError: Cannot read property 'impl' of undefined extensions::messaging:184
通过多次测试发现
alert()
窗口按预期出现- 错误不是每次都会出现,但有时会出现
- 错误发生(当它发生时)在关闭
alert()
窗口 - 如果我等待关闭
alert()
窗口,或者在两次重新加载之间,错误似乎不会发生
错误信息指向这个代码片段:
// 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;
}
});
但我什至无法弄清楚到底是哪个文件受到了影响以及它位于哪个文件夹中。
谷歌刚刚找到了另一个明显相同错误的描述: https://teamtreehouse.com/forum/anyone-else-getting-error-cannot-read-property-impl-of-undefined
该页面的作者认为,错误只是有时发生这一事实指向异步过程的问题,有时重新加载页面时该过程未完成。
我通过切换到使用 console.log()
解决了我的调试任务。但我很感兴趣到底发生了什么错误,以及如何避免它返回到我使用 alert()
函数的错误调试方式。
最佳答案
嘿(:我在这个论坛(https://teamtreehouse.com/forum/anyone-else-getting-error-cannot-read-property-impl-of-undefined)中找到了对你的问题的可能解释。我引用:'undefined is being returned prior to accessing the property impl. Since it's occurring intermittently, that suggests to me that privates( port) 是一个异步操作 - 它允许其他代码继续执行 - 有时 privates 会在访问属性 impl 之前完成,有时它不会。' End quote。尽管这只是一个猜测,但我发现它是合理的
关于javascript - alert() 有时会导致 Chrome 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31866783/