最近在研究backbone的源码,看到了他的extend,感觉写的非常给力,这里记录下
var extend = function(protoProps,staticProps){
// 自己就是parent
var parent = this;
// 定义child
var child;
// 查看
if(protoProps && _.has(protoProps,"constructor")){
child = protoProps.constructor;
}
else{
child = function(){
return parent.apply(this,arguments);
};
}
_.extend(child,parent,staticProps);
var Surrogate = function(){
this.constructor = child;
}
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
if(protoProps){
_.extend(child.prototype,protoProps);
}
child.__super__ = parent.prototype;
return child;
}
//
Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend;