The ECMAScript function does not mind how many parameters are passed in, nor does it cause errors due to inconsistent parameters. In fact, the function body can use the arguments object to receive passed parameters.
Copy codeThe Code is as follows:
Function box (){
Return arguments [0] + '|' + arguments [1]; // obtain the value of each parameter.
}
Alert (box (1, 2, 3, 4, 5, 6); // pass the Parameter
The length attribute of the arguments object to obtain the number of parameters.
Function box (){
Return arguments. length; // get 6
}
Alert (box (1, 2, 3, 4, 5, 6 ));
We can use the length attribute to intelligently determine the number of parameters, and then apply the parameters reasonably.
For example, to implement an addition operation, add all the numbers that are passed in, and the number of numbers is uncertain.
Copy codeThe Code is as follows:
Function box (){
Var sum = 0;
If (arguments. length = 0) return sum; // if no parameter exists, exit
For (var I = 0; I <arguments. length; I ++) {// If yes, accumulate
Sum = sum + arguments [I];
}
Return sum; // return the accumulate result.
}
Alert (box (5, 9, 12 ));
Functions in ECMAScript Do not overload functions like other advanced languages.
Function box (num ){
Return num + 100;
}
Function box (num) {// this function will be executed
Return num + 200;
}
Alert (box (50); // return results