怎么把函数中的 arguments 转成数组?
参考答案
function f() {
const arr1 = Array.prototype.slice.call(arguments); // 经典
const arr2 = Array.from(arguments); // ES6
const arr3 = [...arguments]; // 可迭代
const arr4 = Array.prototype.concat.apply([], arguments);
}
推荐 Array.from(对类数组与可迭代都适用)。
注意:箭头函数没有 arguments,需用 rest 参数 (...args);严格模式下 arguments 与形参不联动。