Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>insert title here</title>
<script type= "Text/javascript" >
/*
Overloading of functions does not exist in *1.js
The number of parameters that the 2.js function defines, and the number of arguments passed when it is executed, can be different.
3.js execution, the actual parameters will be encapsulated into a group arguments
*/
function Add (a) {
return a+10;
}
var add=new Function ("A", "return a+10");
Alert (Add (5));
function Add (num1,num2) {
return num1+num2;
}
var add=new Function ("Num1", "num2", "return num1+num2");
Alert (Add (5,6));
Alert (Add (5));//The result of this invocation is Nan: A function of the two parameters defined after the call
That is, although there is a declaration of Var, in JavaScript, as long as the variable name is the same, the definition will overwrite
The previous definition of ======= concludes that there is no overload of the function in JS.
-------------------overload-----for simulating methods with arguments objects
-Call a different code block based on the number of different parameters, up to 25 parameters
function Addnum () {
alert (arguments.length);
for (Var x=0;x<arguments.length;x++) {
Alert (arguments[x]);
This object can only love the body of the function
}
if (arguments.length==1) {
return arguments[0]+10;
}else if (arguments.length==2) {
return arguments[0]+arguments[1];
}else{
Return "parameter error, please check";
}
}
var value=addnum (10,20,30);
Alert ("Return value of Function:" +value);//The value of the result value is: "Parameter error, please check"
In fact, it is through the judgment of parameters, to implement the function of calling different functions, and return different values; Doing this similarly implements the overloads in Java
But in essence, JS is not overloaded, the same variable, in a different position, if the assignment, will inevitably overwrite the previously declared variables. Of course
This eliminates the relationship between the quantity inside the function and the external variables of the function.
</script>
<body>
</body>