(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”);