Created
March 28, 2011 14:10
-
-
Save xiaojue/890526 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @flieoverview 不基于YUI3的模仿YUI3的base功能的一些原生代码实现 | |
* 首先简述一下,YUI3的base是什么。 | |
* Base是一个YUI3的一个基础类,通过继承Base可以以一种统一的方式创建自己的具有新的属性、并可作为事件目的(Event target)的类。 | |
* | |
* 简单说一下分析结构和思路,在YUI3的base类中,首先继承了Attribute基类,Attribute又继承了Event-custom,也就是EventTarget类,通过他们2个的功能组合又实现的base类 | |
* | |
* 在具体的代码实现中,只按照base的API实现的基本的几个功能,按照自己的思路进行的组织,实现为 | |
* _Event | |
* _Attribute | |
* _Base | |
* | |
* 然后辅助的私有对象为 | |
* _lang | |
* _fn -主要实现继承 | |
* | |
* 由于时间原因,只实现了大概的思路和按照自己的思维进行了代码组织,基本根据base的源码反应了base这个组件的基础作用,也对base组件有一定的认识与了解。 | |
* | |
* YUI3真的很强大,尤其源码认真读起来受益匪浅!感谢圆心这个任务,对我的js提升又有很大帮助! | |
* | |
* @author longxiao.fq | |
* | |
* 2011 3 24 | |
*/ | |
(function(w,undefined){ | |
/** | |
* 定义全局对象BASE | |
*/ | |
w.BASE=(function(){ | |
var ADD = ["toString", "valueOf"], | |
OP = Object.prototype; | |
/*只实现需要用到的*/ | |
var _lang={ | |
isString:function(o){ | |
return o.toString() === '[object String]'; | |
}, | |
isBoolean:function(o){ | |
return o.toString() === '[object Boolean]'; | |
}, | |
isArray:function(o){ | |
return o.toString() === '[object Array]'; | |
}, | |
isFunction:function(o){ | |
return o.toString() === '[object Function]'; | |
}, | |
isPlainObject:function(o){ | |
return o.toString() === '[object Object]' && !o['nodeType'] && !o['setInterval']; | |
} | |
}; | |
/*只实现需要用到的*/ | |
var _fn={ | |
/*_IEEnumFix*/ | |
_IEEnumFix: (-[1,]) ? function(r, s) { | |
var i, fname, f; | |
for (i=0;i<ADD.length;i=i+1) { | |
fname = ADD[i]; | |
f = s[fname]; | |
if (_lang.isFunction(f) && f!=OP[fname]) { | |
r[fname]=f; | |
} | |
} | |
} : function(){}, | |
/*extend*/ | |
extend:function(subc,superc,overrides){ | |
if (!superc||!subc) return; | |
var F = function() {}, i; | |
F.prototype=superc.prototype; | |
subc.prototype=new F(); | |
subc.prototype.constructor=subc; | |
subc.superclass=superc.prototype; | |
if (superc.prototype.constructor == OP.constructor) { | |
superc.prototype.constructor=superc; | |
} | |
if (overrides) { | |
for (i in overrides) { | |
if (overrides.hasOwnProperty(i)) { | |
subc.prototype[i]=overrides[i]; | |
} | |
} | |
_fn._IEEnumFix(subc.prototype, overrides); | |
} | |
} | |
}; | |
/** | |
* Event custom 为base实现自定义事件 | |
* 只实现了两个基础方法:on,_fire | |
*/ | |
var _Event=function(){} | |
_Event.prototype={ | |
EvtList:{}, | |
on:function(type, fn){ | |
var that=this; | |
if(that.EvtList.hasOwnProperty(type)){ | |
that.EvtList[type].fn=fn; | |
if(that.EvtList[type].hasOwnProperty('parameter')) fn(that.EvtList[type].parameter); | |
else fn(); | |
} | |
}, | |
_fire:function(evt,parameter){ | |
this.EvtList[evt]={}; | |
this.EvtList[evt].fn=function(){}; | |
if(parameter) this.EvtList[evt].parameter=parameter; | |
} | |
}; | |
/** | |
* Attribute 为base实现属性的添加,继承自_Event,拥有自定义事件的能力 | |
* 只实现了基础的几个方法:get,set,AttrChange | |
*/ | |
var _Attribute=function(){}; | |
_fn.extend(_Attribute,_Event,{ | |
get:function(key){ | |
var host=this.constructor; | |
return host['ATTRS'][key].value || ''; | |
}, | |
set:function(key,value){ | |
var host=this.constructor, | |
yet; | |
if(key in host['ATTRS']){ | |
yet=host['ATTRS'][key].value; | |
host['ATTRS'][key].value = value; | |
this._fire('AttrChange',{yet:yet,now:value}); | |
} | |
} | |
}); | |
/** | |
* 继承自Attribute,拥有get,set,AttrChange静态属性和Event自定义事件的能力, | |
* 并提供自己的_init 和 _destry 方法 | |
* base | |
*/ | |
var _Base=function(){ | |
var host=this; | |
host._init(); | |
var destructor=function(){}; | |
if(host.destructor) destructor=host.destructor; | |
host.destructor=function(){ | |
host._destry(); | |
destructor(); | |
}; | |
}; | |
_fn.extend(_Base,_Attribute,{ | |
_init:function(){ | |
if(this.initializer) this.initializer(); | |
this._fire('initializer'); | |
}, | |
_destry:function(){ | |
console.log('注销所有自定义事件或者其他一些base类应该理应注销的东西……,注销事件被调用时即被触发。比如清除ATTRS值,等等吧。'); | |
} | |
}); | |
return { | |
extend:_fn.extend, | |
base:_Base | |
}; | |
})(); | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment