arguments对象接收所有实参
function fun() {
console.log(arguments);
console.log(arguments.length);
}
fun(1,2,3,4,5,6,7,8,9);
循环输出所有实参
function fun() {
console.log(arguments);
console.log(arguments.length);
for(var i = 0; i <= arguments.length -1 ;i++){
console.log(arguments[i]);
}
}
fun(1,2,3,4,5,6,7,8,9);
根据传入参数的数量输出不同结果
function sum(a,b,c){
switch(arguments.length){
case 1:
return a;
break;
case 2:
return a + b;
break;
case 3:
return a > b ? a + c : b + c ;
break;
default:
throw new Error("参数个数不能超过3个");
}
}
console.log(sum(1));
console.log(sum(1,2));
console.log(sum(1,2,3));
console.log(sum(1,2,3,4,5));