The method of JS overload implementation is analyzed in this paper. Share to everyone for your reference, specific as follows:
Overload is an important feature of object-oriented language, JS does not really overload, is simulated (because JS is an object-based programming language, not purely object-oriented, it does not have real polymorphism: such as inheritance, overload, rewrite)
When do I use heavy duty?
Example:
function GetDataTop10 () {
alert ("Return first 10 data");
}
Later demand changed, need to get 20 data, and GETDATATOP10 is called by many functions, the whole replacement can but trouble, then you can use overloaded
Which leads to what is Overload: The function name is the same, but it can be distinguished by which one you want to use, rather than the following function with the same name as the previous
Second, how to use?
A built-in object in the Arguments:js that contains the actual arguments passed by the caller (the number of parameters defined when the function declaration does not affect)
function GetData () {
alert ("Return all Data");
}
function Getdatatop (num) {
alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdste () {
var num=arguments.length;
if (!num) {
getData ();
}
else if (num==1) {
getdatatop (arguments[0]);
}
else if (num==2) {
getdatalimit (arguments[0],arguments[1]);
}
Getdste (5)
When a lot of parameters to write a large string, so you can improve
function GetData () {
alert ("Return all Data");
}
function Getdatatop (num) {
alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdste () {
var num=arguments.length;
if (!num) {
getData ();
}
else if (num==1) {
getdatatop.apply (this,arguments);
}
else if (num==2) {
getdatalimit.apply (this,arguments);
}
}
Getdste (5)
What if the number of parameters is the same and the type is different
function GetData () {
alert ("Return all Data");
}
function Getdatatop (num) {
alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdatabytype (type) {
alert ("returns data of type +type+");
}
function Getdste () {
var num=arguments.length;
if (!num) {
getData ();
}
else if (num==1) {
if (typeof arguments[0]== "number") {
getdatatop.apply (this,arguments);
}
else{
getdatabytype.apply (this,arguments);
}
else if (num==2) {
getdatalimit.apply (this,arguments);
}
}
Getdste ("array")
Third, benefits
Conducive to cooperative development
Iv. Disadvantages
"Implementation" too reluctantly, if too many parameters, there will be a lot of if statements, it appears that the code is very messy
For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary
I hope this article will help you with your JavaScript programming.