gpt4 book ai didi

javascript - JS 恢复默认/全局函数

转载 作者:数据小太阳 更新时间:2023-10-29 04:30:20 25 4
gpt4 key购买 nike

这是一个假设性的问题,它确实没有实际用途,但是......

假设您要执行以下操作:

document.open = null;

如何将 document.open 恢复到其原始功能,这可能吗(没有用户创建的临时存储)? document.open 是否以鲜为人知的名称存储在另一个位置?谢谢! :)

最佳答案

覆盖 document.open 直接在 document 对象上创建一个名为 open 的变量/函数。但是,原始函数不在对象本身上,而是在其原型(prototype)上 - 因此您确实可以恢复它。

open 函数来自 HTMLDocument.prototype,因此您可以使用 HTMLDocument.prototype.open 访问它。

要直接调用它,请使用 .call() 指定要使用它的对象:

HTMLDocument.prototype.open.call(document, ...);

您还可以通过简单地分配它来恢复document.open:

document.open = HTMLDocument.prototype.open;

但是,请记住 HTMLDocumentdocument 是宿主对象,通常最好不要弄乱它们 - 特别是在 IE 中,如果你这样做。

关于javascript - JS 恢复默认/全局函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11404200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com