欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS模拟实现ECMAScript5新增的数组方法

程序员文章站 2022-09-30 11:26:58
ecmascript5 新增了十个数组方法,这些方法只有在ie9及以上浏览器中可以被使用,下面是对于这些方法的模拟实现。 一、array.isarray(element)...

ecmascript5 新增了十个数组方法,这些方法只有在ie9及以上浏览器中可以被使用,下面是对于这些方法的模拟实现。

一、array.isarray(element)

  该方法用于判断传入的对象是否为数组类型,返回true和false。

array.newisarray = function(element){
  return object.prototype.tostring.call(element).slice(8,-1).tolocalelowercase() === 'array';
}

二、.indexof(element)

  该方法用于查找传入对象在数组中的位置,并返回该位置,若没有找到则返回-1,该方法不能用于寻找undefined。

  indexof方法可以和~符配合使用。按位运算符~会将传入数字取反并减一,所以-1就会变成0,这时候把它放在判断条件中会被隐式转换为false。

array.prototype.newindexof = function(element){
  var index = -1;
  for(var i = 0; i < this.length; i++){
    if(this[i] === element && this[i] !== undefined){
      index = i;
      break;
    }
  }
  return index;
};
var a = [1,2,3,4,,,5];
console.log(a.newindexof(undefined));

三、lastindexof(element)

  该方法与indexof(element)作用和返回值相同,唯一不同的地方是它是从右向左寻找。

array.prototype.newlastindexof = function(element){
  var index = -1;
  for(var i = this.length - 1; i >= 0; i--){
    if(this[i] === element && this[i] !== undefined){
      index = i;
      break;
    }
  }
  return index;
};
var a = [1,2,3,4,5,2,,,3];
console.log(a.newlastindexof(undefined));

四、foreach(function(element, index, array){})

  遍历数组,参数为一个回调函数,有三个传参:当前元素、当前元素索引、整个数组,该方法会跳过保留缺失成员,不会破坏原数组。

array.prototype.newforeach = function(fn){
  for(var i = 0; i < this.length; i++){
    if(i in this){
      fn(this[i], i, this);
    }
  }
};
var a = [1,2,3,undefined,undefined,4,5,2,3];
a.foreach(function(e, i, arr){
  console.log(e, i, arr);
})

五、every(function(element, index, array){})

  使用传入的回调函数遍历数组,当所有回调都返回true时,every方法返回true,否则返回false。该方法会跳过保留缺失成员,不会破坏原数组。

array.prototype.newevery = function(fn){
  var status = true;
  for(var i = 0; i < this.length; i++){
    if(i in this){
      if(!(status = !!fn(this[i], i, this))){
        break;
      }
    }
  }
  return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newevery(function(){
  console.log(arguments);
  return 1;
}));

 六、some(function(element, index, array){})

  使用传入的回调函数遍历数组,当有回调返回true时,some方法返回true,否则返回false。该方法会跳过保留缺失成员,不会破坏原数组。

array.prototype.newsome = function(fn){
  var status = false;
  for(var i = 0; i < this.length; i++){
    if(i in this){
      if(status = !!fn(this[i], i, this)){
        break;
      }
    }
  }
  return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newsome(function(){
  console.log(arguments);
  return 0;
}));

 七、map(function(element, index, array){})

  使用传入的回调函数遍历数组,使用遍历数组返回的内容组成一个新的数组,该方法会跳过空元素,但是最终结果保留缺失成员的位置,不会破坏原数组。

array.prototype.newmap = function(fn){
  var array = new array(this.length);
  for(var i = 0; i < this.length; i++){
    if(i in this){
      array[i] = fn(this[i], i, this);
    }
  }
  return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newmap(function(element, index, array){
  console.log(arguments);
  return element;
}))

八、filter(function(element, index, array){})

  使用传入的回调函数遍历数组,最终返回一个新数组,该数组中包含所有回调函数返回true的元素,不会破坏原数组。

array.prototype.newfilter = function(fn){
  var array = [];
  for(var i = 0; i < this.length; i++){
    if((i in this) && fn(this[i], i, this)){
      array.push(this[i]);
    }
  }
  return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newfilter(function(element, index, array){
  console.log(arguments);
  return element;
}))

九、reduce(function(accumulator, currentvalue, currentindex, array){})

  使用传入的回调函数遍历数组,返回最后一个回调函数调用的返回值,跳过缺失成员,不会破坏原数组。 

array.prototype.newreduce = function(fn){
  if(this.length === 0){
    throw new typeerror('reduce of empty array with no initial value');
  }
  var result;
  for(var i = 0; i < this.length; i++){
    if(i in this){
      if(!result){
        result = this[i];
      }else{
        result = fn(result, this[i], i, this);
      }
    }
  }
  return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newreduce(function(a,b){
  console.log(arguments);
  return a + b;
}))

十、reduceright(function(accumulator, currentvalue, currentindex, array){})

  该方法作用于reduce相同,唯一区别是它是从右往左开始遍历。

array.prototype.newreduceright = function(fn){
  if(this.length === 0){
    throw new typeerror('reduce of empty array with no initial value');
  }
  var result;
  for(var i = this.length - 1; i >= 0; i--){
    if(i in this){
      if(!result){
        result = this[i];
      }else{
        result = fn(result, this[i], i, this);
      }
    }
  }
  return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newreduceright(function(a,b){
  console.log(arguments);
  return a + b;
}))

以上所述是小编给大家介绍的js模拟实现ecmascript5新增的数组方法,希望对大家有所帮助