如何在JavaScript中为函数定义rest参数?
使用rest参数,您可以将多个参数表示为数组。ES6带来了rest参数,以简化开发人员的工作。对于参数对象,其余参数由三个点…表示,并位于参数之前。
示例
让我们看一下下面的代码片段,以定义rest参数。
<html>
<body>
<script>
function addition(…numbers) {
var res = 0;
numbers.forEach(function (number) {
res += number;
});
return res;
}
document.write(addition(3));
document.write(addition(9,10,11,12,13));
</script>
</body>
</html>热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短