js實現父子類整合的方法
阿新 • • 發佈:2019-02-04
//屬性拷貝
function cpProperties(src,desc){
for(var key in src){if(src.hasOwnProperty(key)){
desc[key] = src[key];
}
}
}
var Base = function(option) {
cpProperties(option,this);
if(!this.ctx){
throw new Error("Should has canvas's ctx!");
}
this.createBg(this.ctx);
}
Base.prototype = {
createBg:function(){
}
}
//供子類重寫
Base.extend = function(option){var a = function(){
Base.apply(this,arguments);
};
cpProperties(Base.prototype,a.prototype);
cpProperties(option,a.prototype);
return a;
}
var LED = Base.extend({
createBg:function(){
alert("123")
}
});