2022-09-18笔试题00
请注意,本文编写于 585 天前,最后修改于 585 天前,其中某些信息可能已经过时。

面试高频手撕代码题

实现一个 forEach 方法

forEach()方法对数组的每个元素执行一次给定的函数。

arr.forEach(function (currentValue, currentIndex, arr) {}, thisArg);

//currentValue  必需。当前元素
//currentIndex  可选。当前元素的索引
//arr           可选。当前元素所属的数组对象。
//thisArg       可选参数。当执行回调函数时,用作 this 的值。
Array.prototype._forEach = function (fn, thisArg) {
  if (typeof fn !== 'function') throw '参数必须为函数';
  if (!Array.isArray(this)) throw '只能对数组使用forEach方法';
  let arr = this;
  for (let i = 0; i < arr.length; i++) {
    fn.call(thisArg, arr[i], i, arr);
  }
};

// test
let arr = [1, 2, 3, 4, 5];
arr._forEach((item, index) => {
  console.log(item, index);
});

// test thisArg

function Counter() {
  this.sum = 0;
  this.count = 0;
}
// 因为 thisArg 参数(this)传给了 forEach(),每次调用时,它都被传给 callback 函数,作为它的 this 值。
Counter.prototype.add = function (array) {
  array._forEach(function (entry) {
    this.sum += entry;
    ++this.count;
  }, this);
  // ^---- Note
};

const obj = new Counter();
obj.add([2, 5, 9]);

console.log(obj.count); // 3 === (1 + 1 + 1)
console.log(obj.sum); // 16 === (2 + 5 + 9)

本文作者:前端小毛

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!