在JavaScript代碼中,我試圖通過存儲的數據重新命名對象。我嘗試使用pathTo,正如本網站所建議的(http://thedesignspace.net/MT2archives/000381.html),但我的控制檯返回「ReferenceError:'pathTo'未定義」。我的代碼看起來是這樣的:通過屬性重新命名變量(JavaScript)
// This code defines the Object constructor Card, used to make the card objects
var Card = function() {
this.face = theFace(this);
this.suit = theSuit(this);
this.value = theValue(this);
};
// This code creates the Deck to be used.
var deck = [];
for (i=0 ; i<52 ; i++) {
deck.push(i);
};
for (i=51 ; i>0 ; i--) {
var random = Math.floor(Math.random()*i);
var temp = deck[random];
deck[random] = deck[i];
deck[i] = temp;
};
// 0-12 is Spades.
// 13-25 is Hearts.
// 26-38 is Clubs.
// 39-51 is Diamonds.
// Now we create the hand of the player and dealer
var player = [];
var dealer = [];
// Now to deal a card to player
player.push(deck.pop());
dealer.push(deck.pop());
// and another
player.push(deck.pop());
dealer.push(deck.pop());
// function theFace gives the face of a card
function theFace(card) {
var faces = ["King","Ace","2","3","4","5","6","7","8","9","10","Queen","Jack"];
return(faces[card%13]);
};
// function theValue uses 'switch' to determine points from a card
function theValue(card) {
var value = card % 13;
switch(value) {
case(0):
case(11):
case(12):
value = 10;
break;
case(1):
value = 11;
break;
default:
value = value;
break;
};
return value;
};
// function theSuit returns the suit of a card
function theSuit(card) {
var suit;
if(card>38) {
suit = "Diamonds";
}else if(card>25) {
suit = "Clubs";
}else if(card>12) {
suit = "Hearts";
}else {
suit = "Spades";
};
return suit;
};
// function toObject the first (numbered) card of of a hand
// and turns it into an Object with the desired properties
function toObject(hand) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card();
card = pathTo[card.suit + card.face];
};
return hand;
};
console.log(player);
toObject(player);
toObject(player);
console.log(player);
我試圖重新命名我從typeof運算轉向===「號」來的typeof ===「對象」的卡,這樣,當我運行代碼多次(因此函數)我沒有手中數組的對象的重複名稱。
這裏是什麼我的控制檯打印的一些例子:
[ 19, 46 ]
ReferenceError: 'pathTo' is undefined
和
[ 31, 18 ]
ReferenceError: 'pathTo' is undefined
必須有辦法做到這一點,我怎麼都找不到。
在函數toObject中,我試圖將手數組中的第一個數字(卡片)變成一個描述其限定符的對象,作爲標準52卡片卡片的卡片。編輯:我只是意識到我甚至沒有推回來。我會嘗試一下,看看它是否會起作用。
EDITEDITEDIT_SOLVED: 我已經做到了!事實證明,我不需要更改名稱,代碼會以某種方式爲我保持獨立。所有我需要做的,以便它可以正常運行是這樣的:
更換
var Card = function() {
this.face = theFace(this);
this.suit = theSuit(this);
this.value = theValue(this);
};
與
var Card = function(card) {
this.face = theFace(card);
this.suit = theSuit(card);
this.value = theValue(card);
};
和
function toObject(hand) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card();
card = pathTo[card.suit + card.face];
};
return hand;
};
與
function toObject(hand) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card (card);
hand.push(card);
};
return hand;
};
感謝您的幫助!
你能用簡單的英語解釋你想用這段代碼實現什麼嗎? – georg 2012-02-10 14:51:01
對不起,我以爲我做到了。我正在做的是讓一個#r代表52張卡片(0-51)中的一張卡片,並在手陣列中將其轉向描述其屬性的對象。但是,我需要爲卡片生成一個名稱,這就是'pathTo'創建一個'ReferenceError'的地方。 – CAD97 2012-02-10 15:50:47
我的主要問題是試圖改變卡的名稱,所以我沒有結束與我的數組中的多個卡對象。 – CAD97 2012-02-10 16:09:15