本文实例讲述了JS实现手写 forEach算法。分享给大家供大家参考,具体如下:手写 forEach手写 forEach手写 forEachforEach()方法对数组的每个元素执行一次提供的函数forEach()
arr.forEach(callback(currentValue [, index [, array]])[, thisArg]);
arr.forEach(callback(currentValue [, index [, array]])[, thisArg]);



callback



currentValue

数组中正在处理的当前元素。

index 可选

数组中正在处理的当前元素的索引。

array 可选

forEach() 方法正在操作的数组。

thisArg 可选

可选参数。当执行回调函数 callback 时,用作 this 的值。





没有返回值


callback



currentValue

数组中正在处理的当前元素。

index 可选

数组中正在处理的当前元素的索引。

array 可选

forEach() 方法正在操作的数组。

thisArg 可选

可选参数。当执行回调函数 callback 时,用作 this 的值。



callback

currentValue

数组中正在处理的当前元素。

index 可选

数组中正在处理的当前元素的索引。

array 可选

forEach() 方法正在操作的数组。

thisArg 可选

可选参数。当执行回调函数 callback 时,用作 this 的值。

currentValue

数组中正在处理的当前元素。index 可选

数组中正在处理的当前元素的索引。array 可选

forEach() 方法正在操作的数组。thisArg 可选

可选参数。当执行回调函数 callback 时,用作 this 的值。没有返回值如果提供了一个 thisArg 参数给 forEach 函数,则参数将会作为回调函数中的 this 值。否则 this 值为 undefined。回调函数中 this 的绑定是根据函数被调用时通用的 this 绑定规则来决定的。thisArgforEachthisthisundefinedthisthis
let arr = [1, 2, 3, 4];

arr.forEach((...item) => console.log(item));

// [1, 0, Array(4)] 当前值


let arr = [1, 2, 3, 4];

arr.forEach((...item) => console.log(item));

// [1, 0, Array(4)] 当前值


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]);
obj.count;
// 3 === (1 + 1 + 1)
obj.sum;
// 16 === (2 + 5 + 9)


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]);
obj.count;
// 3 === (1 + 1 + 1)
obj.sum;
// 16 === (2 + 5 + 9)



每个数组都有这个方法

回调参数为:每一项、索引、原数组
每个数组都有这个方法回调参数为:每一项、索引、原数组
Array.prototype.forEach = function(fn, thisArg) {
var _this;
if (typeof fn !== "function") {

throw "参数必须为函数";
}
if (arguments.length > 1) {

_this = thisArg;
}
if (!Array.isArray(arr)) {

throw "只能对数组使用forEach方法";
}

for (let index = 0; index < arr.length; index++) {

fn.call(_this, arr[index], index, arr);
}
};


Array.prototype.forEach = function(fn, thisArg) {
var _this;
if (typeof fn !== "function") {

throw "参数必须为函数";
}
if (arguments.length > 1) {

_this = thisArg;
}
if (!Array.isArray(arr)) {

throw "只能对数组使用forEach方法";
}

for (let index = 0; index < arr.length; index++) {

fn.call(_this, arr[index], index, arr);
}
};

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》JavaScript数学运算用法总结JavaScript数据结构与算法技巧总结JavaScript数组操作技巧总结JavaScript排序算法总结JavaScript遍历算法与技巧总结JavaScript查找算法技巧总结JavaScript错误与调试技巧总结希望本文所述对大家JavaScript程序设计有所帮助。