我正在写another question的答案。
我不确定我的枚举实现是否正常。我马上就会形容它。请检查一下,告诉我是否可以做得更好。
有问题的小原型(codepen在这里)涉及绘制棋盘和棋子。有6个白棋和6个黑棋子。对于它们中的每一个,我需要保留其名称(如"White Bishop"
)和Unicode代码(类似"\u2654"
)。所以,我定义变量pieces
如下:(还有一块NONE
证明对板上的空方块有用)
var pieces = {
NONE : {name: "None", code: " "},
WHITE_KING : {name: "White King", code: "\u2654"},
WHITE_QUEEN : {name: "White Queen", code: "\u2655"},
WHITE_ROOK : {name: "White Rook", code: "\u2656"},
WHITE_BISHOP : {name: "White Bishop", code: "\u2657"},
WHITE_KNIGHT : {name: "White Knight", code: "\u2658"},
WHITE_POWN : {name: "White Pown", code: "\u2659"},
BLACK_KING : {name: "Black King", code: "\u265A"},
BLACK_QUEEN : {name: "Black Queen", code: "\u265B"},
BLACK_ROOK : {name: "Black Rook", code: "\u265C"},
BLACK_BISHOP : {name: "Black Bishop", code: "\u265D"},
BLACK_KNIGHT : {name: "Black Knight", code: "\u265E"},
BLACK_POWN : {name: "Black Pown", code: "\u265F"},
};
我在一个名为board的数组中保存数据板上所有部分的数据,这是该数组的初始化:
var board =[];
for(var i = 0; i < boardDimension*boardDimension; i++) {
board.push({
x: i % boardDimension,
y: Math.floor(i / boardDimension),
piece: pieces.NONE
});
};
board[0].piece = pieces.BLACK_ROOK
board[1].piece = pieces.BLACK_KNIGHT
board[2].piece = pieces.BLACK_BISHOP
board[3].piece = pieces.BLACK_QUEEN
board[4].piece = pieces.BLACK_KING
board[5].piece = pieces.BLACK_BISHOP
board[6].piece = pieces.BLACK_KNIGHT
board[7].piece = pieces.BLACK_ROOK
board[8].piece = pieces.BLACK_POWN
board[9].piece = pieces.BLACK_POWN
board[10].piece = pieces.BLACK_POWN
board[11].piece = pieces.BLACK_POWN
board[12].piece = pieces.BLACK_POWN
board[13].piece = pieces.BLACK_POWN
board[14].piece = pieces.BLACK_POWN
board[15].piece = pieces.BLACK_POWN
board[6*8 + 0].piece = pieces.WHITE_POWN
board[6*8 + 1].piece = pieces.WHITE_POWN
board[6*8 + 2].piece = pieces.WHITE_POWN
board[6*8 + 3].piece = pieces.WHITE_POWN
board[6*8 + 4].piece = pieces.WHITE_POWN
board[6*8 + 5].piece = pieces.WHITE_POWN
board[6*8 + 6].piece = pieces.WHITE_POWN
board[6*8 + 7].piece = pieces.WHITE_POWN
board[7*8 + 0].piece = pieces.WHITE_ROOK
board[7*8 + 1].piece = pieces.WHITE_KNIGHT
board[7*8 + 2].piece = pieces.WHITE_BISHOP
board[7*8 + 3].piece = pieces.WHITE_QUEEN
board[7*8 + 4].piece = pieces.WHITE_KING
board[7*8 + 5].piece = pieces.WHITE_BISHOP
board[7*8 + 6].piece = pieces.WHITE_KNIGHT
board[7*8 + 7].piece = pieces.WHITE_ROOK
而且,在代码的深处,这些数据的使用方式如下:
svg.append("text")
.attr("x", function (d) {
return d.x*fieldSize;
})
.attr("y", function (d) {
return d.y*fieldSize;
})
.style("font-size", "40")
.attr("text-anchor", "middle")
.attr("dy", "35px")
.attr("dx", "20px")
.text(function (d) {
return d.piece.code;
})
.append("title")
.text(function(d) {
return d.piece.name;
});
我是否以正确的方式定义和使用枚举?
答案 0 :(得分:1)
好吧,开始在Javascript中没有 Enum ,但你可以自己实现它,看看你做过的工作,我认为这是正确的,这是一个很好的尝试。
但如果我们专注于 Enum 的逻辑,我们应该知道 Enum 必须常量及其元素值永远不应该改变所以我们必须寻找更合适的解决方案,因为在 Javascript 中我们可以覆盖任何对象。
经过我的搜索后,我找到了一个可以提供解决方案的Javascript方法,即 Object.freeze() 。
所以在定义你的枚举之后你应该冻结它,所以它永远不会改变:
var pieces = {
NONE : {name: "None", code: " "},
WHITE_KING : {name: "White King", code: "\u2654"},
WHITE_QUEEN : {name: "White Queen", code: "\u2655"},
WHITE_ROOK : {name: "White Rook", code: "\u2656"},
WHITE_BISHOP : {name: "White Bishop", code: "\u2657"},
WHITE_KNIGHT : {name: "White Knight", code: "\u2658"},
WHITE_POWN : {name: "White Pown", code: "\u2659"},
BLACK_KING : {name: "Black King", code: "\u265A"},
BLACK_QUEEN : {name: "Black Queen", code: "\u265B"},
BLACK_ROOK : {name: "Black Rook", code: "\u265C"},
BLACK_BISHOP : {name: "Black Bishop", code: "\u265D"},
BLACK_KNIGHT : {name: "Black Knight", code: "\u265E"},
BLACK_POWN : {name: "Black Pown", code: "\u265F"},
};
var frozenpieces=Object.freeze(pieces);
在这种情况下,frozenpieces
对象将始终保持不变,因此我们可以假设它是一个枚举。