我正在使用JavaScript构建一个国际象棋游戏。我正在采取面向对象的方法,并且在处理JavaScript的继承时遇到一些困难。我想要成为一个“Piece”抽象类,它包含一些领域和基本的getter/setter,比如这件作品是黑色还是白色。那么我想为每种类型片的类,我可以实例像这样:)如何在JavaScript中扩展一个类
var pieceOne = new Pawn();
典当(应具有的所有字段和计件的方法,但有其自己的运动方法和附加字段(比如它是否已经移动,因为这对大多数作品都没有影响)。这是我目前的一块类:
//This object specifies basic information about pieces.
"use strict";
function Piece(color, type, captured, hasMoved) {
this.color = color;
this.type = type;
this.captured = captured;
this.hasMoved = hasMoved;
this.image = color + "_" + type + ".svg";
}
Piece.prototype.getImage = function getImage(){
return this.image;
}
Piece.prototype.isCaptured = function isCaptured(){
return this.captured;
};
我知道,如果我要做出各种一块我可能会消除“类型”字段中的一个子类,但是我怎么能让一个Pawn子类?像这样?
function Pawn() = new Piece(color, captured, hasMoved);
Pawn.prototype.getLegalMoves = function getLegalMoves(){
//return legal moves
}
var pieceOne = new Pawn("black", false, false);