Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带函数参数的Rest算子_Javascript_Rest_Operator Keyword - Fatal编程技术网

Javascript 带函数参数的Rest算子

Javascript 带函数参数的Rest算子,javascript,rest,operator-keyword,Javascript,Rest,Operator Keyword,const sum=(函数(){ “严格使用”; 返回函数和(…参数){ 返回参数reduce((a,b)=>a+b,0); }; })(); log(总和(1,2,3,4))删除所有噪声后,代码为 const sum = (...args) => args.reduce((a, b) => a + b, 0) 那是 接受任意数量的参数(…args) 将这些参数作为数组(args) 对其应用reduce 将累加器初始化为0 在每个reduce步骤中,将当前值b添加到累加器a

const sum=(函数(){
“严格使用”;
返回函数和(…参数){
返回参数reduce((a,b)=>a+b,0);
};
})();

log(总和(1,2,3,4))
删除所有噪声后,代码为

const sum = (...args) => args.reduce((a, b) => a + b, 0)
那是

  • 接受任意数量的参数(
    …args
  • 将这些参数作为数组(
    args
  • 对其应用
    reduce
  • 将累加器初始化为
    0
  • 在每个
    reduce
    步骤中,将当前值
    b
    添加到累加器
    a


方法如下:您已经知道它被称为rest语法,所以您大概对它有所了解。你有什么特别不明白的?你懂什么?
...args  //The rest parameter syntax allows us to represent an indefinite number of arguments as an array.