您的位置:首页 > Web前端 > JavaScript

第一个基础文件 名字是base.js

2012-01-13 21:18 323 查看
2012-01-13

(function(window,undefined){
var _toString = {}.toString,
_is,
_writeScript,
_listUrl,
alfred = {
author : "alfred",
version : 1.01,
global : this,
doc : this.document,
reset : function(it){this.global[it]=alfred;},
log : function(){if(window.console && console.log){return console.log(arguments)}},
isNumber : function(it){return _is(it,"Number")},
isString : function(it){return _is(it,"String")},
isFunction : function(it){return _is(it,"Function")},
isObject : function(it){return _is(it,"Object")},
isArray : function(it){return _is(it,"Array")},
isBoolean : function(it){return typeof it ==='boolean'},
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 = this.scriptMap.method_url,
u2m = this.scriptMap.url_method,
r;
if (!this.scriptMap.visited[m2u[method][0]] && !this.scriptMap.writted[m2u[method][0]]) {
//haven't be visited or write
this.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){
if (url && method && !this.scriptMap.url_method[url]){
this.scriptMap.url_method[url] = method;
for (var i = method.length - 1; i >= 0; i--) {
this.scriptMap.method_url[method[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)
{
to = this;
--i;
}
for (;i<len;i++) {
if ((options=arguments[i])!==null)
{
for (name in options) {
copy = options[name];
src = target[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 _toString.call(it).toLowerCase()==="[object "+type.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;
};
};
$ = alfred;
window.$ = $;
})(window)


base.js

This is the first version for my javascript lean.

I need learn more .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: