function Add (n1,n2) {
return n1+n2;
}
function Add (n1,n2,n3) {
return n1+n2+n3;
}
alert (add); The//nan,js method uses the nearest principle, and since the method does not pass in N3, the result is NaN
JS there is no method overload, how to solve the above problem?
//arguments
Function F1 () {
var sum=0;
for (Var i=0;i<arguments.length;i++) {
Sum+=arguments[i];
}
return sum;
}
Alert (F1 (1,2,3,4,5));
Using arguments, the calculation of chestnuts plus numbers and
function ff (name) {
var sum=0;
for (Var i=1;i<arguments.length;i++) {
Sum+=arguments[i];
}
return arguments[0]+sum;
}
Alert (FF (' chestnut ', 1,2,3,4,5));//Chestnut 15
Name, age
function test (name,age) {
if (!age) {//age No parameter is passed, age is undefined
age=18;
}
Alert (' The name is ' +name+ ' age is ' +age ');
}
Test (' Xiao Ming ');//name is Xiaoming age is 18
The use of arguments in JavaScript