make some class more better and fixed bugs
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 418
(function(window,undefined){var_toString={}.toString,   _is,   _writeScript

(function(window,undefined){
var _toString = {}.toString,
    _is,
    _writeScript,
    _listUrl,
    alfred = {
        author : "alfred",
        version : 1.01,
        global : this,
        doc : this.document,
        reset : function(it){alfred.global[it]=alfred;},
        log : function(){if(window.console && console.log){return console.log(arguments)}},
        isNumber : function(it){return _is(it,/^\[object number\]$/)},
        isString : function(it){return _is(it,/^\[object string\]$/)},
        isFunction : function(it){return _is(it,/^\[object function\]$/)},
        isObject : function(it){return _is(it,/^\[object object\]$/)},
        isArray : function(it){return _is(it,/^\[object array\]$/)},
        isBoolean : function(it){return typeof it ==='boolean'},
        isDom : function(it){return _is(it,/^\[object html\w+\]$/)},
        is : _is,

scriptMap : {
            method_url : {},// method --> url < method : url > < 1 : 1 >
            url_method : {},// url --> method < url : [method,method] > < 1 : n >
            visited : {},
            writted : {},
            wait_write:[]
        },
        require : function(method){
            var m2u = alfred.scriptMap.method_url,
                u2m = alfred.scriptMap.url_method,
                r;
            if(!(m2u[method])){throw "Error : you need setMethod first"}
            if (!alfred.scriptMap.visited[m2u[method][0]] && !alfred.scriptMap.writted[m2u[method][0]]) {
                //haven't be visited or write
                alfred.scriptMap.visited[m2u[method][0]] = true;
                //use an beautiful code in here
                for(; m2u[method][1].length &&(r=m2u[method][1].shift());)
                {
                    require(r);
                }
                _listUrl();
            }
        },
        setMethod : function(url,method,rely){
            var i, method = alfred.isArray(method)? method:[method],
                rely = alfred.isArray(rely)?rely:[rely];
            if (url && !alfred.scriptMap.url_method[url]){
                alfred.scriptMap.url_method[url] = method;
                for (;method.length&&(i=method.shift());) {
                    alfred.scriptMap.method_url[i] = {"0" : url, "1" : rely};
                }
            }
        },
        extend : function(){
            var deep = false,
                len,
                options, name, src, copy, copyIsArray, clone,
                target = arguments[0] || {},
                i = 1;
            len = arguments.length;
            if (!len)
            {
                return this;
            }
            if(alfred.isBoolean(target))
            {
                deep = arguments[0];
                to = arguments[1];
                i = 2;           
            }
            if (!(alfred.isObject(target) || alfred.isFunction(target))) {
                target = {};
            }       
            if (len = i)
            {
                target = this;
                --i;
            }
            for (;i<len;i++) {
                if ((options=arguments[i])!==null)
                {
                    for (name in options) {
                        copy = options[name];
                        src = target[name];
                        if(src){throw "Becareful : you are rewrite the "+_handle+"."+name+"!"}
                        if (copy===target) {continue}
                        if (deep && copy && (alfred.isObject(copy) || (copyIsArray = alfred.isArray(copy))))
                        {
                            if (copyIsArray)
                            {
                                copyIsArray = false;
                                clone = src && alfred.isArray(src) ? src : [];
                            }
                            else
                            {
                                clone = src && alfred.isObject(src) ? src : [];
                            }
                            target[ name ] = alfred.extend( deep, clone, copy );
                        }
                        else if ( copy !== undefined )
                        {
                            target[name] = copy;
                        }
                    };
                };
            };
        }
    };
_is = function(it,type){
    return type.test(_toString.call(it).toLowerCase());
};
_listUrl = function(){
    var l = alfred.scriptMap.visited,i;
    for(i in l){
        alfred.scriptMap.wait_write.push(i);
    }
    alfred.scriptMap.wait_write.reverse();
    _writeScript();
};
_writeScript = function(){
    var head = document.getElementsByTagName("head").item(0);
    for (var i;alfred.scriptMap.wait_write.length&&(i=alfred.scriptMap.wait_write.shift());) {
        var script = document.createElement("script"),
            url = i || "";
        script.setAttribute("async",true);
        script.setAttribute("type","text/javascript");
        script.setAttribute("src",url);
        head.appendChild(script);
        alfred.scriptMap.writted[url] = true;
    };
};
window.alfred = alfred;
})(window)
alfred.reset("$");

 

$.setMethod(“a.js”,[“a.b”,”a.c”],[“a.e”]);

$.require(“a.b”); --|

$.require(“a.c”); –-|----> $.require(“a.b”);

$.require(“a.e”); --|

 

$.isDom(document.body) ==> true;

$.isNumber(a)==> false;

$.isObject({}) ===>true;

$.isArray(“”)===>false;

$.is({},/\[object object\]/) ==> true ;

your can do your own fun with something toString’s result to check the nomal type with it.

$.log(“a”,”b”,”c”); ==> [a,b,c] ===> console.log(“a”,”b”,”c”);

开始 到结束 只是那么个过程。结束了 就忘记吧。
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2