How to implement method overload in JS? and the function parameter problem _javascript technique

Source: Internet
Author: User
All know that in JS there is no way to directly implement the method overload, because in JS if the definition of multiple names are the same, but the number of parameters is not the same method, in fact, only the last method can be real call, the other methods are covered up.

But each function has a special parameter arguments, which can be used to implement the overload of the method.

For example:
Copy Code code as follows:

function Add (firstnumber,sencondnumber) {
return firstnumber+sencondnumber;
}

Only two parameters can be processed, if there are multiple parameters, or there is no parameter, the case of a parameter can not be processed. If no arguments are passed, the firstnumber,sencondnumber are undefined, and if a parameter is passed, it is equivalent to assigning only firstnumber, Sencondnumber remains undefined. Conversely, if more than two parameters are passed, the equivalent of Firstnumber,sencondnumber is assigned, although there are other parameters, but the processing is ignored. If you can get other parameters, you can handle them naturally. At this point you should be able to think of the function of the special parameter arguments, which contains all the parameters passed to the function, you can use it to achieve the effect of the method overload.

The above methods are modified as follows:
Copy Code code as follows:

function Add (firstnumber,sencondnumber) {
if (arguments.length = 0)//No parameters passed
{
return null;
}
else if (arguments.length = 1) {//passing is a parameter
Return firstnumber;//can also be written as return arguments[0];
}
else if (arguments.length = 2)//passed two arguments
{

Return firstnumber+sencondnumber;//can also be written as return arguments[0]+arguments[1];
}

else {
var total=0;
for (var i = 0; i < arguments.length; i++) {
Total=total+arguments[i]
}
return total;
}

}

Of course, the disadvantage of this approach is that the order of the parameters can not be disrupted, if the function implementation depends on the order of the parameters, you must do special processing, such as passing null to occupy.

Because the parameters passed to the function are assigned to each parameter in strict accordance with the order in which the function is defined, if you want to assign only the second parameter, you must pass two arguments, otherwise the value passed is assigned to the first argument and is not assigned to the second argument.

For example, if you want to give sencondnumber values only, but do not want to give firstnumber value, you must call Add (null,2) (of course, the function must handle the case of passing special values), if this call to add (2), in fact, to the Firstnumber pass the value, The equivalent of a call to pass a parameter.

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.