/*
*
*CrazyJs
*
*
*/
(function (window, undefine) {
//局部变量
var crazyJs = function (selector, context) {
return new crazyJs.fn.buildCrazyJs(selector, context);
},
//快速检索
quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/,
//html标签
htmltag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
toString = Object.prototype.toString,
DOMContentLoaded,
//文档对象
documentCJ,
push = Array.prototype.push;
readyFuns = [];
crazyJs.fn = crazyJs.prototype = {
/*crazyJs内部构建起,在构建crazyJs时系统采用这个构建子进行构建对象,这里对象时脚本对象,例如
*crazyJs,
*/
buildCrazyJs: function (selector, context) {
var match;
if (!selector) {
return this;
}
//DOM对象
if (selector.nodeType) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
//匹配选择符
if (typeof (selector) == "string") {
match = quickExpr.exec(selector);
// if (match[1])
// doc = context ? context : document;
//选择单个标签
if (match && match[1]) {
//
tags = htmltag.exec(selector);
//如果是html 标签选择
if (tags) {
//创建一个元素
selector = [document.createElement(tags[1])];
//是否是数组
if (crazyJs.isArray(selector) && t) {
//构建数组
return this.setArray(selector);
}
}
}
//处理选择$("#Id")
else if (match && match[2] && !context) {
element = document.getElementById(match[2]);
if (element) {
this.length = 1;
this[0] = element;
}
this.context = document;
this.selector = selector;
return this;
}
//标签选择
else if (/^\w+$/.test(selector) && !context) {
this.selector = document.getElementsByTagName(selector);
this.context = document;
if (!context && crazyJs.isArray(this.selector)) {
return this.setArray(this.selector);
}
}
} //end if(typeof (selector=="string")
//函数
if (crazyJs.isFunction(selector)) {
//加载函数
return documentCJ.load(selector);
}
}, //end buildCrazyJs
//默认当前长度为0
length: 0,
// selector: "",
/*
*预加载执行脚本
*/
load: function (callback) {
/*
* crazyJs 预加载
* 为执行回调脚本加载做好准备
*/
crazyJs.preLoad();
//如果crazyJs准备就绪则执行回调
if (crazyJs.isReady) {
//执行回调否则进入等待队列
callback.call(document, this);
} else {
readyFuns.push(callback);
}
return this;
}, //end load
setArray: function (elem) {
this.length = 0;
push.apply(this, elem);
return this;
},
attr: function () {
},
each: function (callback) {
if (!callback)
return this;
for (i = 0; i <this. length; i++) {
callback.call(this);
}
}
}//end crazyJs.fn
/*
*
*重力转移,使得构建buildCrazyJs具备crazyJs对象所有的对外属性与方法
*
*/
crazyJs.fn.buildCrazyJs.prototype = crazyJs.fn;
//生产一个文档crazyJS 对象,控制文档
documentCJ = crazyJs(document);
// Cleanup functions for the document ready method
if (document.addEventListener) {
DOMContentLoaded = function () {
document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false);
crazyJs.load();
};
} else if (document.attachEvent) {
DOMContentLoaded = function () {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if (document.readyState === "complete") {
document.detachEvent("onreadystatechange", DOMContentLoaded);
crazyJs.load();
}
};
};
/*
* 继承实现
*
*/
crazyJs.inherit = crazyJs.fn.inherit = function (deepcopy, srcobj, objn) {
var target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
arg,
name,
src,
copy;
//处理深度拷贝
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// 处理字符串或者是数字其他情况
if (typeof target !== "object" && typeof target != "[object Function]") {
target = {};
}
// 如果只有一个参数则把拷贝目标指向当前调用者
if (length === 1) {
target = this;
--i;
}
for (; i < arguments.length; i++) {
arg = arguments[i];
if (arg == target)
continue;
if (arg) {
for (name in arg)
if (!deep)
target[name] = arg[name];
else {
}
} //end if(arg)
} //end for (; i < arguments.length; i++)
return target;
};
//对类库扩展
crazyJs.inherit({
//状态
isReady: false,
//加载
load: function () {
if (!crazyJs.isReady) {
if (!document.body)
return setTimeout(crazyJs.load, 13);
}
crazyJs.isReady = true;
if (readyFuns) {
i = 0;
while (temfn = readyFuns[i++]) {
temfn.call(document, crazyJs);
} //end while
} //end if(readyFuns)
//释放
readyFuns = null;
}, //end of load function
preLoad: function () {
if (document.readyState == "complete")
return crazyJs.load();
// Mozilla, Opera and webkit nightlies currently support this event
if (document.addEventListener) {
// Use the handy event callback
document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
// A fallback to window.onload, that will always work
window.addEventListener("load", crazyJs.load, false);
// If IE event model is used
} else if (document.attachEvent) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", DOMContentLoaded);
// A fallback to window.onload, that will always work
window.attachEvent("onload", crazyJs.load);
}
}, //end of preLoad
//判断对象是否是一个函数,是返回真,否则返回假
isFunction: function (obj) {
return toString.call(obj) == "[object Function]";
}, //end of isFunction
isSimpleObject: function (obj) {
if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval) {
return false;
}
// Not own constructor property must be Object
if (obj.constructor
&& !hasOwnProperty.call(obj, "constructor")
&& !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for (key in obj) { }
return true;
// return key === undefined || hasOwnProperty.call(obj, key);
}, //end of isSimpleObject
isArray: function (obj) {
return toString.call(obj) == "[object Array]";
},
//构建一个数组
//src 数组源
//res 构建
newArray: function (src1, src2) {
var ret = src2 || [];
var i = crazyJs.isArray(src1) ? src.length : 0, j = 0;
if (src1) {
while (src2[j] != null)
src1[i++] = src2[j++];
}
src1.length = i;
return ret;
}
}); //end of inherit;
//暴露对外接口
window.crazyJs = window.$ = crazyJs;
})(window)