按属性重新命名变量(JavaScript)

re-Naming variables by properties (JavaScript)

本文关键字:JavaScript 变量 新命名 属性      更新时间:2023-09-26

在JavaScript代码中,我试图通过存储的数据重新命名对象。我试着按照这个网站的建议使用pathTo(http://thedesignspace.net/MT2archives/000381.html),但我的控制台返回"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牌组的一张牌。编辑:我刚刚意识到我甚至没有把它推回去。我要试试看它是否有效。

editedit_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']

您正在阅读的网站正在记录数组语法。它没有记录一个名为pathTo的函数——没有这样的内置函数,所以引用它会导致ReferenceError。

也许您应该定义一个对象并引用它。