alert() 在 Chrome 中不起作用 [英] alert() not working in Chrome
问题描述
'nuff 说.我完全不知道为什么使用 alert() 不起作用.它在 Firefox 中运行良好,但在 Chrome 中出现该错误.
'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
是 DOMWindow
的一个实例,通过给 window.alert
设置一些东西,正确的实现被遮蔽"了,即当访问 alert
时,它首先在 window
对象上寻找它.通常这是找不到的,然后它会沿着原型链向上查找本机实现.但是,当手动将 alert
属性添加到 window
时,它会立即找到它,不需要沿着原型链向上.使用delete window.alert
,您可以删除窗口自己的属性,并再次暴露alert
的原型实现.这可能有助于解释:
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屋!