JavaScript function namespaces, parameter type overloading implementations

Source: Internet
Author: User

Sometimes the use of Dofunc ({arg1:xxx, arg2:xxx}), inconvenient, or in the parameter table overload, and the overloading of the various things to get dizzy, the result is to try to write such a thing, do not know if there is no place to use:

<! DOCTYPE html>

The arg-func.js used:

(function () {if (! String.prototype.trim) {String.prototype.trim = function () {return this.replace (/(^\s+) | (        \s+$)/g, ");    }; } var TYPE = {unkown:null, UNDEFINED: ' UNDEFINED ', null: ' null ', Boolean: ' Boolean ', Number: ' Number ', STRING: ' Strin    G ', function: ' function ', object: ' object ', array: ' Array '}; function GetType (o) {if (o = = = undefined) {return TYPE. UNDEFINED; } if (o = = = null) {return TYPE. NULL; } 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; Case ' [Object Object] ': Return TYPE.            OBJECT; Default:return O? TYPE. Object:type.        Unkown; }} var _argfunc = {_parse: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];            } argtypes = Argtypes.join (', ');            var nsnn = _argfunc._analysenamespace (name, namespace);            namespace = Nsnn.namespace;            name = Nsnn.name;            var wrapperfunc = Namespace[name]; if (!wrapperfunc) {Wrapperfunc = Namespace[name] = function () {argfunc.apply (wrapperf                UNC, arguments);            }; } if (Wrapperfunc[argtypes]) {Throw ' method already has an overload of the same parameter type.            ‘;            } else {wrapperfunc[argtypes] = func; }}, _analysenamespace:function (name, uppernamespace) {var namespace = uPpernamespace | |            Window            var parts = name.split ('. ');            var name = Parts.pop (); for (var i = 0, part; part = parts[i]; i++) {if (!namespace[part]) {Namespace[part] =                {};            } namespace = Namespace[part];        } return {namespace:namespace, name:name};    }    };        var argfunc = {bind:function (Funcs, prototype) {Argfunc.parse (Funcs, prototype); }, Parse:function (Funcs, namespace) {if (arguments.length = = 3) {_argfunc._parse (Argu            Ments[0], arguments[1], arguments[2]); } else if (GetType (funcs) = = TYPE.                 OBJECT) {for (var format in Funcs) {_argfunc._parse (format, Funcs[format], namespace); }} else if (GetType (funcs) = TYPE. ARRAY) {for (var i = 0, func; func = funcs[i]; i++) {_argfunc._parse (func[0], FUnc[1], namespace);            }}}, Apply:function (func, args) {var argtypes = [];                for (var i = 0, len = args.length; i < Len; i++) {var arg = args[i];            Argtypes.push (GetType (ARG));            } argtypes = Argtypes.join (', ');            func = Func[argtypes];            if (!func) {throw ' does not find an overloaded method matching the parameter type ';            } else {func.apply (this, args);    }        }    };    if (!window[' Argfunc ') {window[' argfunc '] = Argfunc; }})();

JavaScript function namespaces, parameter type overloading implementations

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.