function howMany(... args){其余运算符无需检查
返回“你已经通过”+ args.length +“arguments。”;
}
console.log(howMany(0,1,2)); //你已经传递了3个参数
console.log(howMany(“string”,null,[1,2,3],{})); //你已经传递了4个参数。
args
数组,并允许我们在args
数组上应用map()
, filter()
和reduce()
。 sum
,使其使用rest运算符,并以相同的方式使用任意数量的参数。 sum(0,1,2)
的结果应为3'
testString: 'assert(sum(0,1,2) === 3, "The result of sum(0,1,2)
should be 3");'
- text: 'sum(1,2,3,4)
的结果应为10'
testString: 'assert(sum(1,2,3,4) === 10, "The result of sum(1,2,3,4)
should be 10");'
- text: sum(5)
的结果应为5
testString: 'assert(sum(5) === 5, "The result of sum(5)
should be 5");'
- text: sum()
的结果应为0
testString: 'assert(sum() === 0, "The result of sum()
should be 0");'
- text: sum
函数在args
参数上使用...
spread运算符。
testString: 'getUserInput => assert(getUserInput("index").match(/function\s+sum\s*\(\s*...args\s*\)\s*{/g), "The sum
function uses the ...
spread operator on the args
parameter.");'
```