The ECMAScript function does not care how many arguments are passed in, nor does it error because the parameters are not uniform. In fact, a function body can receive incoming arguments by arguments the object.
Copy Code code as follows:
function box () {
Return arguments[0]+ ' | ' +arguments[1]; Get the value of each parameter
}
Alert (Box (1,2,3,4,5,6)); Passing parameters
The length property of the arguments object can get the number of parameters.
function box () {
return arguments.length; Get 6
}
Alert (Box (1,2,3,4,5,6));
We can use the attribute of length to intelligently judge how many parameters there are, and then apply the parameters reasonably.
For example, to implement an addition, add all the numbers that come in, and the number of numbers is uncertain.
Copy Code code as follows:
function box () {
var sum = 0;
if (arguments.length = 0) return sum;///If there are no arguments, exit
for (var i = 0;i < Arguments.length; i++) {//if any, add
sum = sum + arguments[i];
}
return sum;//returns the cumulative result
}
Alert (Box (5,9,12)); The functions in the
ECMAScript do not have function overloading functions like other high-level languages.
function box (num) {
return num + 100;
}
function box (num) {//will execute this function
return num + 200;
}
Alert (Box (50));//return result