# 数组-手写forEach

forEachmap 类似,唯一不同的是 forEach 是没有返回值的。

# polyfill

Array.prototype.forEach = function(callback, thisArg) {
  if (this == null) {
    throw new TypeError('this is null or not defined');
  }
  if (typeof callback !== "function") {
    throw new TypeError(callback + ' is not a function');
  }
  const O = Object(this);
  const len = O.length >>> 0;
  let k = 0;
  while (k < len) {
    if (k in O) {
      callback.call(thisArg, O[k], k, O);
    }
    k++;
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
上次更新: 1/5/2022, 9:25:14 AM