javascript ES6 - 休息参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6 - 休息参数相关的知识,希望对你有一定的参考价值。


// Here we've added multiple arguments. We want to use the first one as a rate
// to calculate all the other values with. With the rest param we put all the 
// Other arguments in the `amounts` array so we can do all the array-things with them
function convertCurrency(rate, ...amounts) {
    console.log(rate, amounts);
    return amounts.map(amount => amount * rate);
}

const amounts = convertCurrency(1.54, 10, 23, 52, 1, 56);
console.log(amounts);



// Here we've got an array with a name and an ID as the first two values and 
// A bunch of speeds The Flash ran after that. We can easily seperate those
// values by destructuring and using the rest parameter
const speedster = ['The Flash', 101, 400, 384, 985, 302];
// As everything is position based in Arrays, the first two variables will be 
// the first two values of the array as well. The `...speeds` variable with rest
// operator takes in all the other values as an array. 
const [name, id, ...speeds] = speedster;
console.log(name, id, speeds );

const ff4 = ['Mr. Fantastic', 'The Invisible Woman', 'The Thing', 'Human Torch'];
const [captain, assistant, ...team] = ff4;
console.log(captain, assistant, team);

以上是关于javascript ES6 - 休息参数的主要内容,如果未能解决你的问题,请参考以下文章

javascript es6休息算子和箭头函数与三元算子实验

text 休息参数,解构数组和对象[JavaScript]

javascript ...休息参数

ES6/Next: 对象解构与休息 - 分组

JavaScript ES6 - 函数扩展

JavaScript ES6 - 函数扩展