Created
August 1, 2013 13:41
-
-
Save luozhihua/6131436 to your computer and use it in GitHub Desktop.
JavaScript inherit solution
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
;(function(){ | |
function do_inherit(cls_1, parent_1, opt) { | |
var inherited = is_inherited.call(this, cls_1, parent_1), | |
parent = typeof parent_1 === 'function' ? new parent_1(opt) : parent_1; | |
if (!cls_1.supperclass || !inherited) { | |
cls_1.supperclass = parent_1; | |
cls_1.prototype = tbc.extend(parent, cls_1.prototype, { | |
/** | |
* 实例的构造类 | |
* @for Class | |
* @property constructor | |
*/ | |
constructor : cls_1, | |
/** | |
* 实例始化方法 | |
* @for Class | |
* @method INIT | |
* @param {Any Type} [param...] | |
*/ | |
INIT : bind_INIT(cls_1) | |
}); | |
} | |
} | |
function bind_INIT(cls) { | |
return function() { | |
var arg = Array.prototype.slice.call(arguments,1), | |
prt = cls.supperclass.prototype; | |
// 调用父类初始化方法 | |
if (prt && typeof prt.INIT === 'function') { | |
prt.INIT.apply(this, arg); | |
} | |
// 调用自定义初始化方法 | |
if (typeof this.init === 'function') { | |
this.init.apply(this, arg); | |
} | |
return this; | |
} | |
} | |
function is_inherited(cls_1, parent_1) { | |
if (this.supperclass === parent_1) { | |
return true; | |
} | |
var node = this; | |
while (node.supperclass) { | |
if (node.supperclass === parent_1) { | |
return true; | |
} | |
node = node.supperclass; | |
} | |
} | |
/** | |
* 继承类 | |
* @for Function | |
* @method inherit | |
* @return {[type]} [description] | |
*/ | |
Function.prototype.inherit = Function.prototype.inherit || function(parent) { | |
var i, len, cls; | |
Array.prototype.push.call(arguments, this); | |
for (i=1, len=arguments.length; i<len; i++) { | |
cls = arguments[i]; | |
if (parent && parent.constructor === Array && typeof parent[0]=== 'function') { | |
do_inherit.call(this, cls[0], parent, cls[1]); | |
} else { | |
do_inherit.call(this, cls, parent); | |
} | |
parent = cls; | |
} | |
return this; | |
} | |
}()); | |
tbc.Cls1 = function(cls1) { | |
var defaults = { | |
}, | |
options = $.extend({}, defaults, cls1); | |
this.INIT(); | |
} | |
tbc.Cls1.prototype = { | |
cls1_add : function(){}, | |
cls1_remove : function(){}, | |
cls1_edit : function(){} | |
} | |
tbc.Cls2 = function(cls2) { | |
this.INIT(); | |
} | |
tbc.Cls2.prototype = { | |
cls2_add : function(){}, | |
cls2_remove : function(){}, | |
cls2_edit : function(){} | |
} | |
tbc.Cls3 = function(cls3) { | |
this.INIT(); | |
} | |
tbc.Cls3.prototype = { | |
y : 2222222222222, | |
cls3_add : function(){}, | |
cls3_remove : function(){}, | |
cls3_edit : function(){} | |
} | |
// inherit | |
// parent > parent > parent > self | |
tbc.Cls3.inherit(tbc.Event, tbc.Cls1, tbc.Cls2); | |
tbc.Cls4 = function(){ | |
this.INIT(); | |
} | |
tbc.Cls4.inherit(tbc.Cls3, {uuu:222, ooo:3333}); | |
var obj1 = new tbc.Cls1(); | |
var obj2 = new tbc.Cls2(); | |
var obj3 = new tbc.Cls3(); | |
var obj4 = new tbc.Cls4(); | |
obj1.addEvent('11', function(){alert(11);}); | |
obj2.addEvent('22', function(){alert(22);}); | |
obj3.addEvent('33', function(){alert(33); alert(this.y); }); | |
obj1.triggerEvent('11'); | |
obj2.triggerEvent('22'); | |
obj3.triggerEvent('33'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment