JS Array内置方法map, each, every, indexOf,reduce,some的等重构

60 篇文章 0 订阅
1 篇文章 0 订阅

1.重构js数组的map方法

// 重构js数组的map方法
Array.prototype.map = function (fn, context) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  let arr = this, temp = [];
  for (let i = 0; i < arr.length; i++) {
    let result = fn.call(context, arr[i], i, arr);
    temp.push(result);
  }

  return temp;
}

2.重构js数组的forEach方法

// 重构js数组的forEach方法
Array.prototype.forEach = function (fn, context) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  for (let i = 0; i < this.length; i++) {
    fn.call(context, this[i], i, this);
  }
  return this;
}

3.重构js数组的every方法

// 重构js数组的every方法
Array.prototype.every = function (fn, context) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  for (let i = 0; i < arr.length; i++) {
    let result = fn.call(context, this[i], i, this);
    
    return !result ? false : true;
  }
}

4.重构js数组的some方法

// 重构js数组的some方法
Array.prototype.some = function (fn, context) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  for (let i = 0; i < this.length; i++) {
    let result = fn.call(context, this[i], i, this);
    return result ? true : false;
  }
}

5.重构js数组的filter方法

// 重构js数组的filter方法
Array.prototype.filter = function (fn, context) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  let temp = [];
  for (let i = 0; i < this.length; i++) {
    let result = fn.call(context, this[i], i, this);
    
    result ? temp.push( this[i]) : ''
  }
  return temp;
}

6.重构js数组的reduce方法

// 重构js数组的reduce方法
Array.prototype.reduce = function (fn, initialValue) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  let num = 0, total = null;
  if (initialValue !== undefined && initialValue !== null) {
    total = initialValue
  } else {
    total = this[0];
    num = 1;
  }

  for (let i = num; i < this.length; i++) {
    let item = this[i];
    total = fn.call(this, total, item, this);
  }

  return total;
}

7.重构js数组的reduceRight方法

// 重构js数组的reduceRight方法
Array.prototype.reduceRight = function (fn, initialValue) {
  if (typeof fn != 'function') {
    throw new TypeError(`${fn} is not a function`);
  }

  let length = this.length, num = length, total = null;
  if (initialValue !== undefined && initialValue !== null) {
    total = initialValue
  } else {
    total = this[length - 1];
    num = length - 2;
  }

  for (let i = num; i >= 0; i--) {
    let item = this[i];
    total = fn.call(this, total, item, this);
  }

  return total;
}

8.重构js数组的indexOf方法

// 重构js数组的indexOf方法
Array.prototype.indexOf = function (item, start) {
  if (!item) {
    throw TypeError('indexOf need arguments');
  }

  for (let i = start || 0; i < this.length; i++) {
    if (item === this[i]) { return i }
  }

  return -1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值