backbone.js的类继承骨干代码qwrap实现

var Model = function(opt){
this._config(opt);
this.init.apply(this,arguments);
}
QW.ObjectH.mix(Model.prototype,{
init : function(){},
_config : function(opt){
var opt = opt||{};
QW.ObjectH.mix(this,opt,true);
},
attrs : {},
get : function(key){
if (this.attr.hasOwnProperty(key)) {
return this.attr[key];
}
return null;
},
set : function(key,val,override){
if(override){
this.attr[key] = val;
}else{
if (!this.attr.hasOwnProperty(key)) {
this.attr[key] = val;
}
}
}
});


var extend = function(proto, props){
var child = inherits(this, proto, props);
child.extend = this.extend;
return child;
};
var inherits = function(p,proto, props){
var child;
if (proto && proto.hasOwnProperty('constructor')) {
child = proto.constructor;
} else {
child = function(){ p.apply(this, arguments); };
}
function ctor(){};
ctor.prototype = p.prototype;

child.prototype = new ctor;
child.prototype.constructor = child;

if(proto){
QW.ObjectH.mix(child.prototype,proto,true);
}
if(props){
QW.ObjectH.mix(child,props,true);
}
child.__super__ = p.prototype;
return child;
};
Model.extend = extend;
var app = {};
app.model = Model.extend({
init : function(){
alert(1);
}
});
app.model2 = app.model.extend({
init : function(){
alert(2);
}
});
var _app = new app.model2({test:'test'});
posted @ 2012-03-20 17:46  greengnn  阅读(456)  评论(0编辑  收藏  举报