//属性拷贝
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")
}
});
本文介绍了如何使用JavaScript实现父子类的继承,通过属性拷贝函数`cpProperties`以及`extend`方法来创建子类`LED`,并在子类中重写了`createBg`方法,展示了JavaScript面向对象编程中的继承概念。
2097

被折叠的 条评论
为什么被折叠?



