Auxiliary methods for adding overloaded functions to JavaScript

Source: Internet
Author: User

JavaScript overload functions are generally operated by arguments.
For example:
Copy codeThe Code is as follows:
Var afunc = function (){
Args = arguments;
If (args. length = 1 ){
Console. log (1 );
} Else if (args. length = 2 ){
Console. log (2 );
} Else if (args. length = 3 ){
Console. log (3 );
}
}

You can imagine how many if-else statements are required when there are too many reloads (in fact, the number of reloads should not be too large ).
If you want to overload JavaScript Functions, there must be a large amount of code. So can we find a way to make the code clearer and reduce the writing of the same code?
This is why I wrote an article and related code.
By convention, first run the Code:
Copy codeThe Code is as follows:
/** KOverLoad
An auxiliary method for creating overloaded functions.
In fact, this method just helps sort out the overload methods when the parameters are different.
If you need to judge and reload the parameter type, implement it in the provided method.
@ Author ake 2010-05-02
@ Weblog http://www.cnblogs.com/akecn
*/
Var KOverLoad = function (scope ){
This. scope = scope | window; // Add the method to this object by default. This of the method added at the same time points to this object.
This. list = {}; // The place where the overload function is stored.
Return this;
};
KOverLoad. prototype = {
// Add an overloaded method.
// @ Param arg <Function> overload method.
Add: function (arg ){
If (typeof arg = "function "){
This. list [arg. length] = arg; // identifies the storage overload method with the number of parameters. Obviously, if you overload the number of method parameters
}
Return this;
},
// Call this method to create an overloaded function after adding all the overloaded functions.
// @ Param fc <String> Method Name of the overloaded function.
Load: function (fc ){
Var self = this, args, len;
This. scope [fc] = function () {// sets the specified method of the specified scope as an overloaded function.
Args = Array. prototype. slice. call (arguments, 0); // convert the parameter to an Array.
Len = args. length;
If (self. list [len]) {// call the conforming overload method based on the number of parameters.
Self. list [len]. apply (self. scope, args); // The scope and parameters are specified here.
} Else {
Throw new Error ("undefined overload type ");
}
}
}
};

The usage is clear to me:
// This is an optional role object.
Copy codeThe Code is as follows:
Var s = function (){}
S. prototype = {
Init: function (){
Console. log ();
}
}

// The constructor parameter can be of the Object type or other valid types. If this parameter is not specified, it is registered to the window Object and the scope is also window. In fact, it is just the place where the overload method is added.
Copy codeThe Code is as follows:
New KOverLoad (s. prototype). add (function (){
Console. log ("one", a, this)
})
. Add (function (a, B ){
Console. log ("two", a, B, this)
})
. Add (function (a, B, c ){
Console. log ("three", a, B, c, this)
})
. Add (function (a, B, c, d ){
Console. log ("four", a, B, c, d, this)
})
. Load ("func"); // The parameter here is the method name of the overload function to be created.

After completing the preceding operations, s. func is an overloaded function.
We can call the overload function as follows:
Copy codeThe Code is as follows:
Var t = new s ();
T. func (); // throw an error exception. Because no zero parameter is specified
T. func ("o"); // one o Object {}
T. func (1, 2); // two 1 2 Object {}

Simple code. If you have any suggestions or comments, please leave a message.

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.