-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforEach.js
More file actions
39 lines (31 loc) · 763 Bytes
/
forEach.js
File metadata and controls
39 lines (31 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @author songchengen
* @date 2018/7/15
* @description Array.prototype.forEach
*/
/**
* 简单实现数组原型遍历功能
* 原函数
* @param callback
* @param arr
* @param thisArg
*/
const forEach = (callback, arr, thisArg) => {
// 参数校验
if (arr == null) throw new TypeError('arr is null or not defined');
if (Object.prototype.toString.call(callback) !== '[object Function]') throw new TypeError(`${callback} is not a function`);
// ToObject(arr)
const O = Object(arr);
// get(O, 'length') 原:O.length >>> 0
const len = O.length;
// 数组遍历
let k = 0;
while (k < len) {
const Pk = k;
if (Pk in O) {
callback.call(thisArg, O[Pk], k, O);
}
k += 1;
}
};
module.exports = forEach;