仿JQ基础架构,可扩展。
(function(win,doc,fn){
var events = [];//拷贝核心方法
var publicEvent = ["extend","fn"];//公共核心方法
var removefn = [];
var A = "A";//原型库
var $_ = "$_";//库名
win[A] = function(e,contentText){
var query = this.__proto__;
contentText = contentText || [];
contentText.push.apply(contentText,doc.querySelectorAll(e));
query.contentText = contentText;
return query;
};
// ===========方法处理s=========
win[A].fn = {
extend:function(obj){//事件方法处理
for(var i in obj){
this[i] = obj[i];
};
}
};
win[A].extend = function(obj){//核心方法处理
events.push(obj)
for(var i in obj){
this[i] = obj[i];
};
};
// ===========方法处理e=========
// ===========方法扩展s=========
//核心方法扩展
win[A].extend({
noConflict:function(name){//多库共存
var new$ = win[$_];
win[$_] = undefined;
return new$;
},
query:win[A],
query_V:"0.1",
query_Name:A,
query_Conflict:$_,//库名
});
win[A].fn.extend({
})
//事件方法扩展
var eventsFn = ["click"];//event事件数组
for (var i = 0 ; i < eventsFn.length;i++){
win[A].fn[eventsFn[i]] = (function(name){//写入事件
return function (fn){
var context = this.contentText;
for(var i = 0 ; i < context.length;i++){
removefn.push({
context:context,
fn:fn
});
context[i].addEventListener(name,fn);
};
};
})(eventsFn[i]);
win[A].fn["un"+eventsFn[i]] = (function(name){//移除事件
return function (fn){
fn = fn || new Function();
for(var i = 0 ; i < removefn.length;i++){
var index = 0;
for(var j = 0 ; j < removefn[i].context.length ; j++){
if(removefn[i].context[j] == this.contentText[j]){
index++;
};
};
if(index == removefn[i].context.length){
var context = this.contentText;
for(var a = 0 ; a < context.length;a++){
context[a].removeEventListener(name,removefn[i].fn);
};
};
};
fn();
};
})(eventsFn[i]);
};
// ===========方法扩展e=========
win[A].prototype = win[A].fn;
win[A].prototype.fn = win[A].fn;
fn(win,doc,win[A],events,publicEvent);
})(window,document,function(win,doc,A,events,publicEvent){
win[A.query_Conflict] = function(a){
return new A(a);
};
for(var i = 0 ; i < events.length ; i++){
for(var j in events[i]){
win[A.query_Conflict][j] = events[i][j];
};
};
for(var i = 0 ; i < publicEvent.length;i++){
win[A.query_Conflict][publicEvent[i]] = A[publicEvent[i]];
};
win[A.query_Conflict].prototype = A.prototype;
});