计算多个数的和,特别是数的个数不定时使用。将多个实参压缩到rest中,成为一个数组,对数组进行遍历,从而求和。
//定义函数
function mysum(...rest) {
let s = 0;
rest.forEach((item) => (s += item));
return s;
}
//调用函数
console.log(mysum(1, 2, 3, 4, 5));
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!