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/