In javascript, the parameter arguments is used to disguise the method overload, and the javascript function parameter

Source: Internet
Author: User

In javascript, the parameter arguments is used to disguise the method overload, and the javascript function parameter

Methods are overloaded in many object-oriented advanced languages. Javascript has no way to overload this concept. However, we can use the arguments parameter to disguise it as a function overload.

Before simulation, let's take a look at the Code:
Copy codeThe Code is as follows:
// Functions without declared Parameters
Function fun (){
Alert ("Sample Code ");
}
Fun ("James", 100, true); // you have written three actual parameters.

Through the results, we can see that even if we do not define formal parameters when declaring a function, we can also write actual parameters when calling a method. (In fact, formal parameters are written to the programmer when calling a function)

Can we get the actual parameters in the code? The answer is yes. Please refer to the Code:
Copy codeThe Code is as follows:
// Functions without declared Parameters
Function fun (){
Alert (arguments [0]); // obtain the value of the first actual parameter.
Alert (arguments [1]); // obtain the value of the second actual parameter.
Alert (arguments [2]); // obtain the value of the third actual parameter.
Alert (arguments. length); // obtain the actual number of parameters.
Alert ("Sample Code ");
}
Fun ("James", 100, true); // you have written three actual parameters.

Through the code, we can know that arguments (internal attribute) is an array, and its function is to store the actual parameters of the method.

With the above knowledge points, there will be ideas for Simulating Method overloading. We can determine the number of actual parameters to execute different logic code. The simple code is as follows:
Copy codeThe Code is as follows:
Function fun (){
If (arguments. length = 0 ){
Alert ("code without actual parameters executed ");
}
Else if (arguments. length = 1)
{
Alert ("Code for passing in an actual parameter ");
}
Else if (arguments. length = 2)
{
Alert ("Code for passing in two actual parameters ");
}
}
Fun ();
Fun ("James ");
Fun ("Xiao Ming", "Xiaohua ");


Can javascript Functions be overloaded?

The reload of javascript Functions is different from that of java functions.
When defining a JavaScript function, the function name is the identifier of the function object. The number of parameters is only the attribute of the function. It is not feasible to implement overload by defining functions with different numbers of parameters.
When calling a function, js finds the corresponding function object through the function name, and matches the function with the expression parameter list in order according to the parameter defined by the function, removing unnecessary parameters, parameters that are not enough are processed by undefined, and then the function code is executed. Therefore, js overload functions must be implemented by determining the parameter values and types through function code.
When defining a function, you need to put the required parameters at the beginning of the parameter list, and the optional parameters are placed after the required parameters in the parameter list to facilitate function overloading.

How to implement a reload similar to java in javascript

Multiple parameters. Or array parameters.
Then use arguments to judge.

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.