The function body can receive passed parameters through the arguments object. The following is a good example. You can feel that the ECMAScript function does not mind how many parameters are passed in, it will not be wrong because of inconsistent parameters. In fact, the function body can use the arguments object to receive passed parameters.
The 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.
The 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