簡體   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