//..arr返回了一个“打开”的数组。或者说它展开了数组
function howMany(...args) {
return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2)); // 输出:You have passed 3 arguments.
console.log(howMany("string", null, [1, 2, 3], { })); // 输出:You have passed 4 arguments.
//展开操作符可以提升代码的可读性,这对后续的代码维护是有积极作用的。
const arr = [6, 89, 3, 45];
const maximus = Math.max(...arr); // 返回 89
//展开操作符只能够在函数的参数中,或者数组之中使用。
const spreaded = ...arr; // 将会发生语法错误
使用spread运算符展开数组项
最新推荐文章于 2023-12-17 21:30:11 发布