这是一个假设性的问题,它确实没有实际用途,但是...JS恢复默认/全球职能
比方说,你做的事:
document.open = null;
如何将一个恢复document.open到其原始功能,这是可能的(没有用户制作的临时存储)? document.open是否以不知名的名称存储在另一个位置?谢谢! :)
这是一个假设性的问题,它确实没有实际用途,但是...JS恢复默认/全球职能
比方说,你做的事:
document.open = null;
如何将一个恢复document.open到其原始功能,这是可能的(没有用户制作的临时存储)? document.open是否以不知名的名称存储在另一个位置?谢谢! :)
var temp = document.open;
document.open = null;
,然后你
document.open = temp;
改写document.open
恢复本来的机能产生直接的document
对象命名open
变量/功能。但是,原始函数不在对象本身上,而是它的原型 - 所以你确实可以恢复它。
open
功能从HTMLDocument.prototype
,因此您可以使用HTMLDocument.prototype.open
访问它。
直接调用它,使用.call()
指定对象上使用它:
HTMLDocument.prototype.open.call(document, ...);
还可以还原document.open
它通过简单地分配给它:
document.open = HTMLDocument.prototype.open;
但是,请记住HTMLDocument
因此document
是宿主对象,通常不要惹他们 - 尤其是在IE中,如果你这样做的话,事情可能会失灵。
啊哈!很高兴知道,谢谢! :) 回应您的编辑...迷人!我从来不知道这一点,再次感谢您的信息! :)我会尽快接受这一点 – 2012-07-09 22:40:10
很高兴知道:+1 – fcalderan 2012-07-09 22:44:06
对于'alert'这样的事情,你会怎么做呢?即使用自定义函数覆盖'window.alert'是相当简单的,但回滚是具有挑战性的,而不保留临时引用:http://jsfiddle.net/ovfiddle/kcLBd/ – 2012-07-09 22:50:23
delete document.open;
这不是直观的,但使用上自定义函数删除关键字将恢复原来的功能,只要至少为原型还没有被覆盖。
实施例:
> console.log
function log() { [native code] }
> console.log = function() { }
function() { }
> console.log("Hello world");
undefined
> delete console.log;
true
> console.log("Hello world");
Hello world
工程与document.open和其他内置函数相同的方式。
'delete alert'和'delete window.alert'都会失败,就像'window.alert = window.prototype.alert'在http://jsfiddle.net/ovfiddle/kcLBd失败一样。除此之外,很好的答案。 – 7vujy0f0hy 2017-05-04 07:16:56
有趣的 - 比方说,你没有温度!有另一种方法吗? – 2012-07-09 22:36:42
您需要以某种方式创建参考...例如运行一个立即自动执行匿名函数传递document.open作为参数 – fcalderan 2012-07-09 22:38:43
我最想知道的是,浏览器是否自己存储第二个引用 – 2012-07-09 22:39:12