Implementation of JavaScript namespaces, function parameter type overloading

Source: Internet
Author: User
Tags string format

The sudden impulse to write something that can be considered in the case of Func ({arg1:xxx, arg2:xxx}) is not applicable.

<! DOCTYPE html>

Arg-func.js:

(function () {if (! String.prototype.trim) {String.prototype.trim = function () {return this.replace (/(^\s+) | (        \s+$)/g, ");    }; } var TYPE = {empty: ' Empty ', Boolean: ' Boolean ', Number: ' Number ', string: ' String ', Function: ' function ', ARRAY: ' AR    Ray ', object: ' object '}; function GetType (o) {if (o = = = undefined) {return TYPE. EMPTY; } if (o = = = null) {return TYPE. OBJECT; } switch (typeof (O)) {case TYPE. Boolean:return TYPE.            BOOLEAN; Case TYPE. Number:return TYPE.            number; Case TYPE. String:return TYPE.            STRING; Case TYPE. Function:return TYPE.        FUNCTION; } switch (Object.prototype.toString.call (o)) {case ' [Object Array] ': Return TYPE.            ARRAY; Default:return O? TYPE. Object:type.        EMPTY;                }} var argfunc = {findfunc:function (args, Funclink) {if (!args | | args.length = = 0) { Return Funclink? Funclink._fuNc:null;            } if (!funclink) {return null; } var index = arguments[2] | |            0;            var argtype = GetType (Args[index]);            var func, nextfunc = Funclink[argtype];                if (index + 1 < args.length) {func = Argfunc.findfunc (args, Nextfunc, index + 1); if (!func) {nextfunc = Funclink[type.                    OBJECT];                Func = Argfunc.findfunc (args, Nextfunc, index + 1);                }} else {func = Nextfunc? nextfunc._func:null; if (!func) {nextfunc = Funclink[type.                    OBJECT]; Func = Nextfunc?                Nextfunc._func:null;        }} return func;            }, Applyfunc:function (Wrapperfunc, args) {var funclink = Wrapperfunc._funcs;            var func = Argfunc.findfunc (args, funclink); if (!func) {throw ' does not find parameter typeMatching overloaded methods ';            } else {func.apply (this, args); }}, Analysenamespace:function (FullName, uppernamespace) {var namespace = Uppernamespace | | wi            Ndow;            var parts = fullname.split ('. ');            var name = Parts.pop (); for (var i = 0, part; part = parts[i]; i++) {if (namespace[part] = = = undefined) {names                Pace[part] = {};            } namespace = Namespace[part];        } return {namespace:namespace, name:name}; }, Parsesingle:function (format, func, namespace) {var LP = Format.indexof (' ('), RP = Format.indexof ('            )‘);            var name = format.substring (0, LP);            var argtypes = format.substring (LP + 1, RP). Split (', ');            for (var i = 0, len = argtypes.length; i < Len; i++) {Argtypes[i] = Argtypes[i].trim (). Split (/+/) [0]; } if (argtypes.length = = 1 && Argtypes[0].length = = 0) {argtypes.pop ();            } var nsnn = argfunc.analysenamespace (name, namespace);            namespace = Nsnn.namespace;            name = Nsnn.name;            var wrapperfunc = Namespace[name]; if (Wrapperfunc = = = undefined) {Wrapperfunc = Namespace[name] = function () {ARGFUNC.A                Pplyfunc (Wrapperfunc, arguments);                };            Wrapperfunc._funcs = {};            } var funclink = Wrapperfunc._funcs;                    for (var i = 0, argtype; argtype = argtypes[i]; i++) {if (funclink[argtype] = = = undefined) {                Funclink[argtype] = {_func:null};            } Funclink = Funclink[argtype];        } Funclink._func = func;                }, Parsearray:function (Funcs, namespace) {for (var i = 0, func; func = funcs[i]; i++) { Argfunc.parsesingle (Func[0], func[1], namespace); }}, Parseobject:function (Funcs, namespace) {for (var. format in Funcs) {Argfun            C.parsesingle (format, Funcs[format], namespace);    }        }    }; Argfunc.parseobject ({' Argfunc.parse (object Funcs, Object namespace) ': function (Funcs, namespace) {Arg        Func.parseobject (Funcs, namespace);        }, ' Argfunc.parse (Object funcs) ': function (funcs) {argfunc.parseobject (Funcs, window); }, ' Argfunc.parse (array Funcs, object namespace) ': function (Funcs, namespace) {Argfunc.parsearray (func        s, namespace);        }, ' Argfunc.parse (array funcs) ': function (funcs) {Argfunc.parsearray (Funcs, window);            }, ' Argfunc.parse (string format, function func, object namespace) ': function (format, func, namespace) {        Argfunc.parsesingle (format, func, namespace); }, ' Argfunc.parse (string format, function func) ': function (fOrmat, func) {argfunc.parsesingle (format, func); }    });}) ();

Implementation of the JavaScript namespace, function parameter type overloading

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.