利用原型链添加数组判断该字段是否存在数组中

废话不多说,直接上代码

Array.prototype.isRepeat = function(str){
  var i = this.length;
  while(i){
    i--;
    if(this[i]==str){
      return true;
    }
  }
  return false;
}

###############用法
let arr = [1,2,3,4,5,6,7];
var str = 4;
console.log(arr.isRepeat (str))  //true

猜你喜欢

转载自blog.csdn.net/qq_42363090/article/details/93486846