Javascript achieves overload through pattern matching

Source: Internet
Author: User

Just as infinte put forward that "more elegant compatibility" is also related to this issue (we will see it later)

In the helper of function in the youa script library, add mode matching that supports heavy load.CopyCodeThe Code is as follows :/**
* The overload method of function parameters is overload to perform mode matching on function parameters. The default dispatcher supports * and... And ?, "*" Represents a parameter of any type, "..." represents multiple parameters of any type ,"? "Generally used ",?... "Indicates zero or any number of parameters.
* @ Method overload
* @ Static
* @ Optional {dispatcher} is used to match the function for parameter distribution.
* @ Param {func_maps} lists the functions that are called according to the matching conditions.
* @ Return {function} the loaded Function
*/
Overload: function (dispatcher, func_maps ){
If (! (Dispatcher instanceof function )){
Func_maps = dispatcher;
Dispatcher = function (ARGs ){
VaR ret = [];
Return map (ARGs, function (o) {return GetType (o)}). Join ();
}
}

Return function (){
Var key = DISPATCHER ([]. Slice. Apply (arguments ));
For (var I in func_maps ){
VaR pattern = new Regexp ("^" + I. replace ("*", "[^,] *"). replace ("... ",". * ") +" $ ");
If (pattern. Test (key )){
Return func_maps [I]. Apply (this, arguments );
}
}
};
},

Functionh. overload includes two parameters: one is the dispatcher function that processes matching conditions (which can be defaulted), and the other is a set of function ing tables, by default, the dispatcher function generates a string based on the actual called parameter type. For example, if the three called parameters are 10, "A", and [1, 2], "Number, string, array. During the implementation of pattern matching, a regular expression is generated based on each "key" in the function ing table. This regular expression is used to match the return value of the dispatcher function, call the processing function corresponding to this key. Otherwise, the next key is matched in sequence, for example: Copy code The Code is as follows: getex: function (OBJ, prop, returnjson ){
VaR ret, proptype = objecth. GetType (PROP );
If (proptype = 'array '){
If (returnjson ){
Ret = {};
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [prop [I] = objecth. getex (OBJ, prop [I]);
}
} Else {
// Getex (OBJ, props)
Ret = [];
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [I] = objecth. getex (OBJ, prop [I]);
}
}
} Else {
// Getex (OBJ, Prop)
VaR keys = (prop + ""). Split (".");
Ret = OBJ;
For (VAR I = 0; I & lt; keys. length; I ++ ){
Ret = RET [Keys [I];
}
If (returnjson ){
VaR JSON = {};
JSON [prop] = ret;
Return JSON;
}
}
Return ret;
},

in this case, the IF Value of "all evil" can be optimized to copy Code the code is as follows: getex: functionh. overload ({
"*, array, *": function (OBJ, prop, returnjson) {
If (returnjson) {
ret = {};
for (VAR I = 0; I & lt; prop. length; I ++) {
RET [prop [I] = objecth. getex (OBJ, prop [I]);
}< BR >}else {
ret = [];
for (VAR I = 0; I & lt; prop. length; I ++) {
RET [I] = objecth. getex (OBJ, prop [I]);
}< BR >}< br> return ret;
},
"*, String ,*": function (OBJ, prop, returnjson) {
var keys = (prop + ""). split (". ");
ret = OBJ;
for (VAR I = 0; I & lt; keys. length; I ++) {
ret = RET [Keys [I];
}< br> If (returnjson) {
var JSON ={};
JSON [prop] = ret;
return JSON;
}< br> return ret;
}< BR >}),

OK, this form may seem better to some people, but it can be further improved --Copy codeThe Code is as follows: getex: functionh. Overload (function (ARGs ){
Return "prop is" + objecth. GetType (ARGs [1]);
},{
"Prop is array": function (OBJ, prop, returnjson ){
If (returnjson ){
Ret = {};
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [prop [I] = objecth. getex (OBJ, prop [I]);
}
} Else {
// Getex (OBJ, props)
Ret = [];
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [I] = objecth. getex (OBJ, prop [I]);
}
}
Return ret;
},
"Prop is string": function (OBJ, prop, returnjson ){
VaR keys = (prop + ""). Split (".");
Ret = OBJ;
For (VAR I = 0; I & lt; keys. length; I ++ ){
Ret = RET [Keys [I];
}
If (returnjson ){
VaR JSON = {};
JSON [prop] = ret;
Return JSON;
}
Return ret;
}
}),

There is also the third parameter "annoying", which is also processed together --Copy code The Code is as follows: getex: functionh. Overload (function (ARGs ){
Return "prop is" + objecth. GetType (ARGs [1]) + "and returnjson is" + ARGs [2];
},{
"Prop is array and returnjson is true": function (OBJ, prop, returnjson ){
Ret = {};
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [prop [I] = objecth. getex (OBJ, prop [I]);
}
Return ret;
},
"Prop is array and returnjson is false": function (OBJ, prop, returnjson ){
Ret = [];
For (VAR I = 0; I & lt; prop. length; I ++ ){
RET [I] = objecth. getex (OBJ, prop [I]);
}
Return ret;
},
"Prop is string and returnjson is true": function (OBJ, prop, returnjson ){
VaR keys = (prop + ""). Split (".");
Ret = OBJ;
For (VAR I = 0; I & lt; keys. length; I ++ ){
Ret = RET [Keys [I];
}
VaR JSON = {};
JSON [prop] = ret;
Return JSON;
},
"Prop is string and returnjson is false": function (OBJ, prop, returnjson ){
VaR keys = (prop + ""). Split (".");
Ret = OBJ;
For (VAR I = 0; I & lt; keys. length; I ++ ){
Ret = RET [Keys [I];
}
Return ret;
}
}),

For example, browser sniffing and feature detection can also be used in the same way (of course, this form has advantages and disadvantages, so you can weigh it yourself )--Copy codeCode: Foo = functionh. Overload (function (){
Return MSIE? "Ie": "notie ";
},{
"Ie": function (){...}
"Notie": function (){...}
});

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.