javascript - 如何获取此中的源代码

标签 javascript

var a = ["one", "two", "three"];
 console.log(a.indexOf("two")); // output = 1

 a.indexOf = function(val) {
  return 'HAHAHA';
};

console.log(a.indexOf("two")); // output = 'HAHAHA'

有哪些方法可以恢复indexOf的功能?

console.log(a.indexOf("two")); // output = 1

最佳答案

您只需重新分配来自Array.prototype的原始函数即可。 .

var a = ["one", "two", "three"];
 console.log(a.indexOf("two")); // output = 1

a.indexOf = function(val) {
  return 'HAHAHA';
};

console.log(a.indexOf("two")); // output = 'HAHAHA'

a.indexOf = Array.prototype.indexOf; // re-assign the original function
                                     // which is inherited from Array.prototype.

console.log(a.indexOf("two")); // output = 1

关于javascript - 如何获取此中的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138458/

相关文章:

javascript - 如何使用 Prototype 突出显示表格行?

javascript - Js 类将这个(类)传递给 jquery datepicker

javascript - 如何访问 Angularjs 中 init 函数中 .then 中设置的值?

javascript - 使用 JavaScript 显示隐藏的 div

javascript - 在 jQuery UI 日期选择器中显示日期旁边的附加文本

javascript - Vue js改变和动画元素的位置

javascript - 使用Fetch API POST到localhost:3000

javascript - 调用存储在 Typescript 变量中的方法

javascript - 动态创建 QML ObjectModel 元素

javascript - 按钮上的禁用似乎没有生效