Posts Tagged ‘javascript 面向对象 object-oriented 闭包 继承’
jQuery面向对象插件abe-oo.js
星期一, 三月 28th, 2011
好久不写博文了,主要是懒得写,好多东西在Google上能查到的,觉得没必要再写抄一遍~~~
今天写了个jQuery的面向对象的插件,其实跟jQuery没什么联系,是好久以前研究javascript的面向对象写的一个简单OO框架吧,只是最近习惯用jQuery了,就把它改了一下,弄成了jQuery的插件了。
直接上代码吧~~实现的原理没什么好说的,就是利用js的闭包和prototype。基本实现了OO的功能,当然与真正的OO还是有差距,但这个是目前我能想的比较好的实现了。
如果有更好的方法,欢迎给我邮件Abraham1@163.com,大家共同讨论进步~~
/**
* jQuery面向对象插件,通过$.inherit(inheritClass, baseClass)函数绑定继承关系。
* @author Abraham
* @email Abraham1@163.com
* @blog http://www.yuhanghome.net
* @address Software Institute, Nanjing University
*/
/**********************************example********************************
function C1(msg){
this.msg=msg;
console.log('c1');
}
C1.prototype.hello=function(){
console.log("Hello C1");
}
function C2(msg){
this.base(msg);
console.log("c2:"+this.msg);
}
C2.prototype.hello=function(){
this.callBase("hello");
$.dprint("Hello C2");
this.callBase("hello");
}
$.inherit(C2,C1);
function C3(msg){
this.base(msg);
console.log("c3:"+this.msg);
}
C3.prototype.hello=function(){
this.callBase('hello');
console.log("hello c3:"+this.msg);
}
$.inherit(C3,C2);
window.onload=function(){
var t=new C3("abraham1@163.com");
t.hello();
}
************************************************************************************/
(function($){
$.extend({
/**
* 绑定继承关系,使用了javascript闭包性质,使得baseType可以使用
* @param {Object} inheritClass 继承类
* @param {Object} baseClass 父类
*
*/
inherit:function(inheritClass, baseClass){
//首先把父类的prototype中的函数继承到子类中
for (var pFunc in baseClass.prototype) {
var sp = inheritClass.prototype[pFunc];
//如果子类中没有这个函数,添加
if (typeof sp === 'undefined') {
inheritClass.prototype[pFunc] = baseClass.prototype[pFunc];
}
//如果子类已经有这个函数,则忽略。以后可使用下面的callBase函数调用父类的方法
}
//保存继承树,当有多级继承时要借住继承树对父类进行访问
inheritClass.__base_objects__=new Array();
inheritClass.__base_objects__.push(baseClass);
if(typeof baseClass.__base_objects__ !=='undefined'){
for(var i=0;i<baseClass.__base_objects__.length;i++)
inheritClass.__base_objects__.push(baseClass.__base_objects__[i]);
}
/**
* 执行父类构造函数,相当于java中的this.super()
* 不使用super是因为super是ECMAScript保留关键字.
* @param {arguments} args 参数,可以不提供
*/
inheritClass.prototype.base = function(args){
var baseClass=null;
if(typeof this.__inherit_deep__==='undefined' ){
this.__inherit_deep__=0;
}else{
this.__inherit_deep__++;
}
baseClass=inheritClass.__base_objects__[this.__inherit_deep__];
if(typeof args==="undefined" || args==null){
baseClass.call(this);
}else if (args instanceof Array === true ) {
baseClass.apply(this, args);
}else{
var _args=new Array();
for(var i=0;i<arguments.length;i++)
_args.push(arguments[i]);
baseClass.apply(this,_args);
}
this.__inherit_deep__--;
}
/**
* 给继承的子类添加调用父函数的方法
* @param {string} method 父类的函数的名称
* @param {arguments} args 参数,可以不提供
*/
inheritClass.prototype.callBase = function( method, args){
var baseClass=null;
if(typeof this.__inherit_deep__==='undefined' ){
this.__inherit_deep__=0;
}else{
this.__inherit_deep__++;
}
baseClass=inheritClass.__base_objects__[this.__inherit_deep__];
var med = baseClass.prototype[method];
if (typeof med === 'function') {
if(typeof args==="undefined" || args==null){
med.call(this);
}else if (args instanceof Array === true) {
med.apply(this,args);
}else {
var _args=new Array();
//从位置1开始,因为第0位参数是method的名称
for(var i=1;i<arguments.length;i++){
_args.push(arguments[i]);
}
med.apply(this, _args);
}
}else{
throw "There is no method:"+method+" in baseClass";
}
this.__inherit_deep__--;
}
}
});
})(jQuery);