- 当在JS方法中我们不能确定参数数量的情况时 用来表示这些参数:
function sum(...theArgs) { return theArgs.reduce((previous,current) => { return previous + current } ); } console.log(sum(1,2,3)); //expected output:6 console.log(sum(3,3,3,3)); //expected output:12
- ...theArgs是一个真正的Array实例 -> 你可以在上面直接使用所有的数组方法:
function checkRule(...theArgs){ var sorted = theArgs.sort(); return sorted; } console.log(checkRule(1,5,2,7)); //expected output:[1,2,5,7]