Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 598 Bytes

11.rest-params.md

File metadata and controls

25 lines (22 loc) · 598 Bytes

剩余参数

ES6的剩余参数可以帮助很简单的将剩余参数组合为一个数组,这在获取函数参数的时候非常有用。

应用场景

  • 对函数参数的处理
function sum(...numbers) {
    return numbers.reduce((prev, curr) => prev + curr, 0);
}
sum(2,3);
function converCurrency(rate, ...amounts) {
    return amounts.map(amount => amount * rate)
}
console.log(converCurrency(0.8, 25, 45));
  • 变量的解构
const player = ['dp', 123, 5.4, 6.7, 3.4, 7.8, 8.9];
const [name, id, ...scores] = player;
console.log(name, id, scores);