The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*
*1.js does not have overloading of functions
2. The number of formal parameters when defining a js function can be different from the number of actual parameters passed during execution.
3.js will be executed, the real parameters will be encapsulated into 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 call is NaN: because the function with two parameters defined after the call
//That is, although there is a declaration of var, as long as the variable name is the same in JavaScript, the subsequent definition will be overwritten as long as the variable name is the same.
//The previous definition ======= The conclusion is that there is no overloading of functions in js.
//--------------------------------------------
//- Call different code blocks according to different number of 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 function body
}
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(" function return value: "+value);//The value of the result value is: "The parameter is incorrect, please check"
//In fact, it is through the judgment of parameters to call different function functions and return different values; this similar implementation of overloading in Java
//But essentially, there is no overload in js. The same variable appears in different locations. If assigned, the variables declared above will inevitably be overwritten. certainly
//This excludes the relationship between the quantities inside the function and the variables outside the function.
</script>
</head>
<body>
</body>
</html>