我正在用JavaScript构建一个纸牌游戏,以增加我的网络编程排序,我遇到了JavaScript Prototype继承问题.我的设计有一个基本卡类,包含任何卡需要的所有功能和数据.设计本身相对灵活,因此只需通过更改存储的数据,大约25%的卡可以使用基类.我需要做的是创建从Card继承所有内容的新类 - 包括数据 - 但是在不触及基类函数的情况下覆盖可用函数的一小部分.
我一直在尝试使用原型继承来实现这一目标,但是这会改变基类,它不仅会使用Card类,而且还会从基类继承所有其他函数.
我需要的是一种设计模式,它允许我仅对从Card继承的类重写函数.这在JavaScript中可行吗?
编辑...
对不起,这是一个例子,我们应该首先添加这个.
从基卡类开始.
function Card(){
this.cardID = 0;
this.name = '';
this.imageID = 0;
this.imageURL = '';
this.imageAlt = '';
etc....
}
Card.prototype.init = function( inID
, inName
, inImageID
, inImageURL
, inImageAlt
, inImageHorizontal
etc...
){
this.cardID = inID;
this.name = inName;
this.imageID = inImageID;
this.imageURL = inImageURL;
this.imageAlt = inImageAlt;
}
Card.prototype.whenPlayed = function(){
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在我的孩子班:
ChildCard.prototype = new Card();
ChildCard.constructor = ChildCard;
function ChildCard(){};
ChildCard.prototype.whenPlayed = …Run Code Online (Sandbox Code Playgroud)