How to overload objects through arguments objects in JavaScript

Source: Internet
Author: User

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>

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.