奇怪的JavaScript代码,可以';没有console.log()就无法生存
我得到了一个非常奇怪的代码:没有调试,没有工作我几乎为之疯狂 代码将在Chrome中显示通知。代码中有很多注释 看这个!!重要和奇怪!!,下一行是“console.log(_notification);”,不能省略,如果省略,则任何事件绑定都无法工作 虽然现在代码工作正常,但我只是好奇,为什么我不能删除“console.log(_notification);”奇怪的JavaScript代码,可以';没有console.log()就无法生存,javascript,html,notifications,Javascript,Html,Notifications,我得到了一个非常奇怪的代码:没有调试,没有工作
也许您可以包含以下代码
这是取自html5样板项目的一个检查,如果
控制台.log可以调用。最终,我没有解决这个问题,也没有找到答案。我只是把代码,它工作得很好,除了打印一个日志信息 我认为这可能是括号的问题。以下是代码的相关部分:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId)
_notification.replaceId = _replaceId;
// !!IMPORTANT&&WEIRD!! remove next line no events will work
console.log(_notification);
}
如果删除console.log行,它将变为:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId)
_notification.replaceId = _replaceId;
}
这可能会让JavaScript引擎感到困惑,因为您有一个无括号的if语句,但后面有一个结束括号。您应该尝试在if语句中添加括号,使其看起来像:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId) {
_notification.replaceId = _replaceId;
}
}
呃,如果插件用于在chromes调试器中显示通知,为什么要删除console.log?似乎是有意删除代码“console.log(_notification);”,而绑定的所有事件(如单击、关闭等)都无法工作。我无法删除“console.log(_notification);”,因为它将导致事件无法正常工作。作为代码,不会弹出警报对话框。console.log
只需调用\u notification
上的toString
方法,并将结果写入浏览器中的JavaScript控制台。移除它不会有什么区别。它一定与线程有关。。。我在IE上也遇到了类似的问题,如果我删除了带有alert()
的一行,一些与iframe相关的代码就不再有效了。尝试摆弄setTimeout(fn,0)
,或将事件绑定到代码中的其他位置。不幸的是,我知道的不多
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId) {
_notification.replaceId = _replaceId;
}
}