How to implement overloaded _javascript techniques for objects through arguments objects in JavaScript

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.