如何在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>热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短