alert()在Chrome中不起作用 [英] alert() not working in Chrome
问题描述
'nuff说。我完全不知道为什么使用alert()不行。它在Firefox中完美工作,但在Chrome中出现此错误。
window.alert = null;
alert('test'); //失败
delete window.alert; // true
alert('test'); // win
窗口
是> DOMWindow
,并通过设置 window.alert
,正确的实现被屏蔽,即当访问 alert
它首先在窗口
对象上查找它。通常情况下,这是找不到的,然后它通过原型链找到原生实现。但是,当手动将警报
属性添加到窗口
时,它会立即发现并且不需要上升原型链。使用 delete window.alert
,您可以删除窗口自己的属性,并再次公开 alert
的原型实现。这可能有助于解释:
window.hasOwnProperty('alert'); // false
window.alert = null;
window.hasOwnProperty('alert'); // true
delete window.alert;
window.hasOwnProperty('alert'); // false
'nuff said. I have absolutely no clue why using alert() there wouldn't work. It works perfectly in Firefox, but gives that error in Chrome.
window.alert = null;
alert('test'); // fail
delete window.alert; // true
alert('test'); // win
window
is an instance of DOMWindow
, and by setting something to window.alert
, the correct implementation is being "shadowed", i.e. when accessing alert
it is first looking for it on the window
object. Usually this is not found, and it then goes up the prototype chain to find the native implementation. However, when manually adding the alert
property to window
it finds it straight away and does not need to go up the prototype chain. Using delete window.alert
you can remove the window own property and again expose the prototype implementation of alert
. This may help explain:
window.hasOwnProperty('alert'); // false
window.alert = null;
window.hasOwnProperty('alert'); // true
delete window.alert;
window.hasOwnProperty('alert'); // false
这篇关于alert()在Chrome中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!