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

标签 javascript debugging methods overriding prototype

我重写了 Element 原型(prototype)上的许多方法,以便我可以添加自定义 Hook ,如下所示:

Element.prototype._method = Element.prototype.method;
Element.prototype.method = function(){
  this._method.apply(this, arguments);
  // custom callback
}

在某些时候,我想恢复原来的方法,所以我这样做:

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

但是,当在节点上调用 method 元素时,在 IE8 中似乎会抛出 Invalid procedure call or argument 错误。我是否错误地恢复了原来的方法?

最佳答案

看来IE8有这个问题,而且不太容易解决,但你可以尝试对Element.prototype进行delete来恢复覆盖。

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

关于javascript - 在 IE8 中恢复被覆盖的 JavaScript 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643695/

相关文章:

javascript - 在运行时创建对象?

javascript - HTML:在不重新加载页面的情况下更改 URL?

c# - 使用 Visual Studio 在 C# 中仅从某个时间点开始调试

ruby-on-rails - 用于元素引用和赋值的 Ruby 方法

javascript - json_decode 返回 NULL 和有效的 json

Javascript::如何将关联数组的键获取到数组变量?

在 Linux 和 Windows 中用 C 语言编码。任何适当的面向调试的以 C 为中心的 IDE?

c# - 有没有办法*完全*禁用编辑并继续?

java - JUnit 测试 : invoking parent class protected method

java - 共享使用的静态方法与实例方法