无法强制要求一个 function
接受必填参数。不过,如果不使用 currentValue
参数,则使用 forEach
没有任何意义;您并不一定需要使用其他参数。所有参数仍然传递给 function
,如果打印出 arguments
,就可以看到。
a = [1, 2, 3, 4, 5];
a.forEach(function () {
console.log("Number of arguments:", arguments.length);
console.log("Current value:", arguments[0]);
console.log("Index:", arguments[1]);
console.log("Original array:", arguments[2]);
})
currentValue
参数的情况下使用forEach
没有意义。 - Unmitigateda = [1, 2, 3, 4, 5];
a.forEach(function (currentValue) {
console.log(currentValue, 5);
})
.forEach
无法因为它需要或不需要的参数数量而失败。如果您使用比声明更多的参数调用函数,则不会发生任何事情。 - VLAZ