JavaScript implements overloaded _javascript techniques through pattern matching

Source: Internet
Author: User
Just infinte students to put forward "more elegant compatibility" in fact, and this problem has a certain connection (we will see later)

In the helper of function in Youa's script library, add a pattern match that supports overloading
Copy Code code as follows:

/**
* Function parameter overload method overload, the function parameter pattern matching. The default dispatcher support * and ... and?, "*" denotes an argument of any type, "..." for multiple arguments of any type, "?" Generally used in ",?..." to represent 0 or any number of parameters
* @method Overload
* @static
* @optional {Dispatcher} is used to match the function that the parameter distributes
* @param {Func_maps} based on matching list of functions that accept calls
* @return {function} overloaded functions
*/
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 handles matching criteria (default), and the other is a set of function mapping tables, and the default dispatcher function is to generate a string based on the type of argument that is actually invoked. For example, the three parameters called 10, "a", [1,2] will generate "Number,string,array", when the specific implementation pattern matching, will be based on the function map of each "key" to generate a regular expression, Use this regular expression to match the return value of the dispatcher function, and if it matches, call the handler function for the key, otherwise the next key will be matched, for example:
Copy Code code 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 "evil" if can be optimized to:
Copy Code code 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]);
}
} else {
ret = [];
for (var i = 0; I & lt; prop.length; i++) {
Ret[i] = Objecth.getex (obj, prop[i]);
}
}
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]];
}
if (Returnjson) {
var json = {};
Json[prop] = ret;
return JSON;
}
return ret;
}
}),

OK, this form may seem better to some people than it used to be, but it can actually go a little further-
Copy Code code 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;
}
}),

and the third parameter of "nasty", which is also dealt with altogether--
Copy Code code as follows:

GetEx:FunctionH.overload (function (args) {
Return ' prop is ' + objecth.gettype (args[1]) + "and Returnjson is" +args[2];
},{
"Prop is array and Returnjson are 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 are 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, the same pattern can also be used (of course, this form has pros and cons, the user to weigh it himself)--
Copy Code code as follows:

Foo = functionh.overload (function () {
Return MSIE? "IE": "Notie";
},{
"IE": function () {...}
' Notie ': function () {...}
});

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.