Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> Insert title here </title>
<Script type = "text/javascript">
/*
* 1.js does not include function overloading.
2. The number of parameters in the js function definition can be different from the number of real parameters passed during execution.
3. During execution of js, the actual parameters are encapsulated into arguments.
*/
Function add (){
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 two parameter functions defined after the call
// Even though there is a var declaration, if the variable name in javascript is the same, the defined variable will overwrite
// The previously defined ====== concluded that there is no function overload in js.
// ------------------- Overload using the arguments object simulation method -----
//-Depending on the number of parameters, different code blocks can be called. A maximum of 25 parameters can be called.
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. Check ";
}
}
Var value = addNum (10, 20, 30 );
Alert ("function return value:" + value); // The result value is: "parameter error. Check"
// In fact, different function functions are called and different values are returned through parameter judgment. In this way, the java overload is implemented similarly.
// But in essence, js does not carry the same variable, and it appears at different positions. If it is assigned a value, it will inevitably overwrite the previously declared variable. Of course
// Eliminate the relationship between the internal volume of the function and the external variable of the function.
</Script>
</Head>
<Body>
</Body>
</Html>