Skip to content

Instantly share code, notes, and snippets.

@welefen
Created March 31, 2014 10:22
Show Gist options
  • Save welefen/9889394 to your computer and use it in GitHub Desktop.
Save welefen/9889394 to your computer and use it in GitHub Desktop.
thinkjs里的继承
/**
* 动态创建一个类
* 提供了继承、扩展、调用父级别方法等方法
* @return {[type]} [description]
*/
global.Class = function (prop) {
var cls = function () {
function T(args) {
if(typeof this.init === 'function'){
//获取init返回值,如果返回一个promise,可以让后续执行在then之后
this.__initReturn = this.init.apply(this, args);
}
return this;
}
var t = arguments.callee;
T.prototype = t.prototype;
T.constructor = t;
return new T(arguments);
};
cls.extend = function(pro){
extend(this.prototype, pro);
return this;
};
cls.inherits = function(superCls){
util.inherits(this, superCls);
return this;
};
//调用父级方法
cls.prototype.super_ = function(name, data){
if (!this[name]) {
return false;
};
if (!isArray(data)) {
data = [data];
};
var super_ = this.constructor.super_;
while(1){
if (this[name] === super_.prototype[name] && super_.super_) {
super_ = super_.super_;
}else{
break;
}
}
var method = super_.prototype[name];
delete super_.prototype[name];
var ret = method.apply(this, data);
super_.prototype[name] = method;
return ret;
};
extend(cls.prototype, prop);
return cls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment