Javascript uses the arguments parameter to disguise the method for overloading

Source: Internet
Author: User

Javascript uses the arguments parameter to disguise the method for overloading

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:

The 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:

The 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:

The 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 ");

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.