在JavaScript代码中,我试图通过存储的数据来重命名对象。我尝试按照此站点(http://thedesignspace.net/MT2archives/000381.html)的建议使用pathTo,但是控制台返回“ ReferenceError:'pathTo'未定义”。我的代码如下所示:
// 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
之前(或在尝试使用它查找卡片之前),给每个对象一个合适的衣服和一个脸。例如,您可以编写: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']