1. 获取函数形参和实参的长度
function test(a, b){
console.log(test.length); // 形参长度
console.log(arguments.length); // 实参长度
}
test(1, 2, 3);
结果
2. 获得函数的实参(对象)
function test(a, b){
console.log(arguments);
}
test(1, 2, 3);
结果
注意:函数的实参和形参可以不想等
function test(a, b, c){
console.log(a, b, c);
}
test(1, 2);
function test1(a, b){
console.log(a, b);
}
test1(1, 2, 3);
结果
例子:未知实参长度,求实参的和
function sum(){
var a = 0;
for(key in arguments){
a += arguments[key];
}
console.log(a);
}
sum(1, 2, 3, 4, 5);
结果
其他的函数形参实参相关,请看下面文章的34-39
项
点击进入文章