繁体   English   中英

如何在此源代码

[英]How to source code in this

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM