"删除" - 恢复本地功能不适用于更改的原型,那么如何? [英] "delete" - restore native function not working for changed prototype, how then?

查看:146
本文介绍了"删除" - 恢复本地功能不适用于更改的原型,那么如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  window.open = function(a,b,c)
{
alert(2);
}

然后您可以只是

 删除window.open 

原始功能,但:如果你改变它的原型,如下所示:

 window .__ proto __。open = function(a,b,c)
{
alert(3);
}

然后 delete won 't做任何事情= \\ \\任何想法如何恢复它现在?

解决方案

window.open 更改为其他内容,例如使用 window.open ='something else'; ,然后你就是 shadowing 打开方法;

//查找window.open(在原型链中) ....
window.open; // Found,result =='something else'
window.__proto__.open; //不能再被访问(被上一行遮蔽)

在调用 delete window.open 删除'something else',原来的方法再次变得可见,因为它从来没有从原型链中消失。



但是,如果您修改了原型中的打开方法,例如 window .__ proto__.open = bogus; ,那么您不能轻易恢复旧的方法。因此,要再次获得打开的窗口行为,您需要在替换之前保留对原始方法的引用,

  var original_open = window.open; 
window.__proto__.open ='bogus';
// ....任何....
//现在恢复它:
window.__proto__.open = original_open;

或借用另一个窗口,例如使用一个临时的新框架:

  var frame = document.createElement('iframe'); 
document.body.appendChild(frame);
window.__proto__.open = frame.contentWindow.open;
frame.parentNode.removeChild(frame);

这个想法很荒谬:你不应该打破内置方法


if you change native function like this:

window.open= function (a,b,c)
{
    alert(2);
}

then later you can just

delete window.open

and it would restore original function, BUT:

if you change its prototype like this:

window.__proto__.open= function (a,b,c)
{
    alert(3);
}

then delete won't do anything =\ any ideas how to restore it now?

解决方案

When you change window.open to something else, e.g. using window.open = 'something else';, then you're shadowing the open method from the prototype;

// Looking up window.open (in the prototype chain)....
window.open;           // Found, result == 'something else'
window.__proto__.open; // Not accessible any more (shadowed by previous line)

After invoking delete window.open to delete 'something else', the original method becomes visible again, because it had never disappeared from the prototype chain.

But if you've modified the open method on the prototype, e.g. window.__proto__.open = bogus;, then you cannot easily restore the old method. So, to get the "open window" behavior again, you need to either keep a reference to the original method before replacing it,

var original_open = window.open;
window.__proto__.open = 'bogus';
// .... whatever ....
// Now restore it:
window.__proto__.open = original_open;

Or borrow it from another window, e.g. using a temporary new frame:

var frame = document.createElement('iframe');
document.body.appendChild(frame);
window.__proto__.open = frame.contentWindow.open;
frame.parentNode.removeChild(frame);

This whole idea is ridiculous though: You should not break built-in methods.

这篇关于"删除" - 恢复本地功能不适用于更改的原型,那么如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆