Sah*_*an 3 html javascript function
function add(args_list){
var total;
return totatl; // Output should be Total of args list
}
add(25,25,10); // 60
Run Code Online (Sandbox Code Playgroud)
我如何使用无限参数制作此功能
这称为Rest 参数
function add(...theArgs) {
const sum = theArgs.reduce((acc, arg) => acc + arg, 0);
console.log('Sum is', sum);
}
add(); // 0
add(5); // 1
add(5, 6, 7); // 3Run Code Online (Sandbox Code Playgroud)
注意:theArgs 是一个数组,但您可以像往常一样将参数传递给函数,而不是作为数组。