Blogger Information
Blog 47
fans 0
comment 3
visits 45048
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用rest参数自定义求和函数
江流
Original
583 people have browsed it

计算多个数的和,特别是数的个数不定时使用。将多个实参压缩到rest中,成为一个数组,对数组进行遍历,从而求和。

  1. //定义函数
  2. function mysum(...rest) {
  3. let s = 0;
  4. rest.forEach((item) => (s += item));
  5. return s;
  6. }
  7. //调用函数
  8. 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!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post