一道 JavaScript 填空题

填空,使后面的 alert(0) 能正确弹出,至少列举两种不同思路。

window.alert = function () {};
______;
alert(0);

解:

方法一:

window.alert = function () {}; delete window.alert; alert(0);

delete 操作符从入门到精通:https://developer.mozilla.org/en/JavaScript/Reference/Operators/delete

有个值得注意的地方,文中提到了操作符的返回值时,描述如下:

Returns false only if the property exists and cannot be deleted. It returns true in all other cases.

仅当属性存在并且不可被删除,则返回 false,否则一律返回 true

方法二:
创建一个 iframe ,获取 iframe 的 window.alert 给当前页面的 window.alert

来自:http://www.mangguo.org/a-javascript-fill-in-the-blank/

金牌狙击手

52html5是一个web前端|html5资源平台,为广大html5开发者及爱好者提供html5相关的教程、资讯、html5游戏、html5教程等,并涉及css3、javascript前端知识。