Array在ES5新增的方法中,参数都是function类型,默认有传参,forEach方法中的function回调支持3个参数,第1个是遍历的数组内容;第2个是对应的数组索引,第3个是数组本身。
因此,我们有:
1
2
3
|
[].forEach(
function
(value, index, array) {
// ...
});
|
对比jQuery中的$.each方法:
1
2
3
|
$.each([],
function
(index, value, array) {
// ...
});
|