Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在IE8中恢复被覆盖的javascript方法_Javascript_Debugging_Methods_Overriding_Prototype - Fatal编程技术网

在IE8中恢复被覆盖的javascript方法

在IE8中恢复被覆盖的javascript方法,javascript,debugging,methods,overriding,prototype,Javascript,Debugging,Methods,Overriding,Prototype,我覆盖了元素原型上的许多方法,因此我可以添加如下自定义钩子: Element.prototype._method = Element.prototype.method; Element.prototype.method = function(){ this._method.apply(this, arguments); // custom callback } 在某些情况下,我希望恢复原始方法,因此我会: Element.prototype.method = Element.proto

我覆盖了
元素
原型上的许多方法,因此我可以添加如下自定义钩子:

Element.prototype._method = Element.prototype.method;
Element.prototype.method = function(){
  this._method.apply(this, arguments);
  // custom callback
}
在某些情况下,我希望恢复原始方法,因此我会:

Element.prototype.method = Element.prototype._method;

但是,当在节点上调用
方法
元素时,它似乎会在IE8中抛出
无效过程调用或参数
错误。还原原始方法是否有误?

IE8似乎存在此问题,不太容易解决,但您可以尝试在
元素上
删除
。prototype
以还原被覆盖的

var old = Element.prototype.getElementsByTagName;
Element.prototype.getElementsByTagName = old;
// alert(document.body.getElementsByTagName('script').length); // this throws Error
delete Element.prototype.getElementsByTagName;
alert(document.body.getElementsByTagName('script').length); // Now it works as expected

+1因为这是一个有趣的问题。然而,我怀疑真正的答案将是重新编写代码,以不同的方式做事。