Home  >  Article  >  Web Front-end  >  How to implement method overloading in js? And function parameter issues_javascript skills

How to implement method overloading in js? And function parameter issues_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:27:371126browse

We all know that there is no way to directly implement method overloading in js, because if multiple methods with the same name but different numbers of parameters are defined in js, only the last method can actually be called, and all other methods will be overwritten. dropped.

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

For example:

Copy code The code is as follows:

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

can only process two parameters. If there are multiple parameters or no parameters, one parameter cannot be processed. . If no parameters are passed, firstnumber and sencondnumber are undefined. If a parameter is passed, it is equivalent to assigning a value to firstnumber only, and sencondnumber is still undefined. On the contrary, if more than two parameters are passed, it is equivalent to firstnumber and secondnumber being assigned values. Although there are other parameters, they are ignored during processing. If other parameters can be obtained, it can be processed naturally. At this point, you should be able to think of the special parameters arguments of the function. This contains all the parameters passed to the function. You can use it to achieve the effect of method overloading.

The above method is modified as follows:
Copy the code The code is as follows:

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

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 method is that the order of parameters cannot be disrupted. If the function implementation depends on the order of parameters, special processing must be performed, such as passing null as a placeholder.

Since the parameters passed to the function are assigned to each parameter strictly in the order in which the function is defined, if you only want to assign a value to the second parameter, you must pass two parameters, otherwise the value passed is actually assigned The first parameter is given, but no value is assigned to the second parameter.

For example, if you only want to pass a value to sencondnumber, but do not want to pass a value to firstnumber, you must call Add(null,2) like this (of course the function must handle the case of passing special values ​​inside), if you call Add(2) like this , in fact, the value is passed to firstnumber, which is equivalent to calling the case where a parameter is passed.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn