数组迭代方法polyfill

参考

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array

Array.prototype.forEach

  1. 描述:为数组中的每个元素执行一次回调函数。
  2. 特点:
  • 不直接改变原数组
  • 返回 undefined
  • 不可链式调用
  • 不可中止或跳出循环,除非抛出异常
  1. 使用:
// currentValue 正在处理的当前元素
// index 正在处理的当前元素的索引
// array 数组本身
// thisArg 执行回调函数用的this,可选
arr.forEach(function (currentValue: any, index: number, array: []): void{
  //...do something
},thisArg:{})
  1. polyfill:
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(callback, thisArg) {
    var newThis;
    var index;

    // this就是调用arr.forEach的arr
    // 校验arr,callback
    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    if (arguments.length > 1) {
      newThis = thisArg;
    }

    var arr = Object(this);

    // >>>为无符号移位,移位前将不是number类型的数据转换成number,将number转为无符号的Uint32类型
    // Unit32类型转换:不能转为number就为0,非整数先转为整数,如果是正数返回正数,负数返回负数+2^32
    // >>>0保证len为数字类型且为正整数,缺省值为0
    var len = arr.length >>> 0;

    index = 0;

    // 循环每个元素执行回调函数
    while (index < len) {
      var currentValue;

      if (index in arr) {
        currentValue = arr[index];

        // currentValue, index, arr是给回调函数的参数
        callback.call(newThis, currentValue, index, arr);
      }

      index++;
    }
  };
}

Array.prototype.map

  1. 描述:创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
  2. 特点:
  • 不直接修改原数组
  • 返回 callback 执行后的返回值组合的新数组
  • 不可中止或跳出循环
  • 可链式调用
  1. 使用:
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
 // Return element for new_array
}[, thisArg])
  1. polyfill:
if (!Array.prototype.map) {
  Array.prototype.map = function(callback, thisArg) {
    var newThis = thisArg || void 0,
      newArr,
      index;

    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    if (Object.prototype.toString.call(callback) != '[object Function]') {
      throw new TypeError(callback + ' is not a function');
    }

    var arr = Object(this);

    var len = arr.length >>> 0;

    newArr = new Array(len);

    index = 0;

    while (index < len) {
      var currentValue, mappedValue;

      if (index in arr) {
        currentValue = arr[index];

        mappedValue = callback.call(newThis, currentValue, index, arr);

        newArr[index] = mappedValue;
      }

      index++;
    }

    return newArr;
  };
}

Array.prototype.filter

  1. 描述:创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
  2. 特点:
  • 返回执行 callback 后返回 true 或者等价于 true 的元素组成的新数组
  • 不会直接改变原数组
  1. 使用:

var new_array = arr.filter(function callback(currentValue[, index[, array]]) {
 // Return element for new_array
}[, thisArg])
  1. polyfill:
if (!Array.prototype.filter) {
  Array.prototype.filter = function(callback, thisArg) {
    'use strict';
    if (!((typeof callback === 'Function' || typeof callback === 'function') && this)) {
      throw new TypeError();
    };

    var len = this.length >>> 0,
      newArr = new Array(len),
      arr = this,
      c = 0, // newArr的索引
      i = -1; // arr的索引,从-1开始++i
    if (!thisArg) {
      // 没有传入this,直接循环执行callback
      while (++i !== len) {
        if (i in arr) {
          // 如果callback执行返回true,则将当前条赋值给newArr
          if (callback(arr[i], i, arr)) {
            newArr[c++] = arr[i];
          }
        }
      }
    } else {
      while (++i !== len) {
        if (i in this) {
          if (callback.call(thisArg, arr[i], i, arr)) {
            newArr[c++] = arr[i];
          }
        }
      }
    }

    newArr.length = c;
    return newArr;
  };
}

注:可中止循环的有:简单循环、for…of 循环、every()、some()、find()、findIndex(),数组的其他迭代方法 polyfill 可以根据以上方法的写法进行改造。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值