在 JavaScript 代码中,我试图通过存储的数据重命名对象。我尝试按照本网站 (http://thedesignspace.net/MT2archives/000381.html) 的建议使用 pathTo,但我的控制台返回“ReferenceError: 'pathTo' is undefined”。我的代码看起来像这样:
// 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==="number"重命名为 typeof==="object"以便当我多次运行代码时(因此函数)我没有重复的名称手数组中的对象。
以下是我的控制台正在打印的一些示例:
[ 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;
};
感谢您的帮助!
最佳答案
正如@mattbornski 指出的那样,JavaScript 中没有pathTo
对象,据我所知,任何浏览器都不支持它。那篇文章似乎省略了他们的这部分代码。
不过,根据您尝试使用它的方式,您需要对代码进行一些更改:
像这样创建一个 pathTo
对象:
pathTo = {};
或
pathTo = new Object();
然后,在尝试将每个新的 Card
对象添加到 pathTo
之前(或者在尝试使用它查找卡片之前)给每个新的 Card
对象一个花色和一张脸.例如,您可以这样写:
myCard = new Card(23);
或
myCard = new Card('spade','2');
最后,当你的牌既有正面又有花色时,将它添加到 pathTo
对象中,如下所示:
pathTo[this.suit + this.face] = this; // use this inside the Card constructor
或
pathTo[myCard.suit + myCard.face] = myCard; // use this with an instance of a Card object
然后您可以通过以下方式查找卡片:
pathTo['spade2']
关于javascript - 按属性重命名变量 (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9229099/