javascript - 按属性重命名变量 (JavaScript)

标签 javascript

在 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/

相关文章:

javascript - 如何一个一个地同时加载和渲染数据

javascript - AngularJS - Controller 内的函数没有像我在简单示例中所期望的那样被调用

javascript - 避免 jsPlumb 中的连接和元素重叠

javascript - Kotlin 相当于 JavaScript 中的 "export default"

Javascript 通过循环添加事件监听器

javascript - 在我可以添加/删除的 Javascript 中存储数字数组的简单方法是什么?

javascript - 命名和未命名匿名 Javascript 函数之间的区别

javascript - “string”无法分配给 Nullable<T>

javascript - 如何禁止 postman 中的 API 执行?

javascript - 如果它不存在,我想在列表中推送一个项目。如果该项目已在列表中,则删除该项目