JavaScript:如何使用嵌套对象的变量名创建嵌套对象?

时间:2016-02-18 02:24:05

标签: javascript object

如何使用变量创建嵌套对象。在这种情况下,我有一个在全局上下文中创建的名为hands的对象。现在我有x个玩家(在这种情况下是3个),我每手牌都有5张牌。

所以我希望双手对象看起来像手[playerNum] [cardInst] [ADECard]作为最终结果。我已经习惯了Perl Hashes,这在Perl中非常简单。

然而,无论我如何尝试将物品放入手中,我都看不到卡片。请参阅下面的代码。 var hands = {};在函数外声明。我错过了什么?



function dealCards() {
    console.log("Dealing Cards\r\n");
    var numPlayers = 3;
    var curCard;

    for (i=0;i<5;i++) {
        var cardInst = i.toString();

        for (pn =0;pn<numPlayers;pn++){
            // get current value for pn and store it
            var playerNum = pn.toString();

            //get card off of game deck
            curCard = gameDeck.shift();

            //add card info to variables
            var dcardID = baseDeck[curCard].id;
            var dcardName = baseDeck[curCard].name;
            var dcardText = baseDeck[curCard].text;
            var dcardImg = "none";

            //create new card object
            var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);



            hands[playerNum][cardInst] = dealtCard;
            console.log("blah");
        }
    }
    console.log(hands);
}

function card(id,name,text,img){
    this.id = id;
    this.name = name;
    this.text = text;
    this.img = img;
}
&#13;
&#13;
&#13;

var hands = {};



function dealCards() {
    console.log("Dealing Cards\r\n");
    var numPlayers = 3;
    var curCard;

    for (i=0;i<5;i++) {
        var cardInst = i.toString();

        for (pn =0;pn<numPlayers;pn++){
            // get current value for pn and store it
            var playerNum = pn.toString();

            //get card off of game deck
            curCard = gameDeck.shift();

            //add card info to variables
            var dcardID = baseDeck[curCard].id;
            var dcardName = baseDeck[curCard].name;
            var dcardText = baseDeck[curCard].text;
            var dcardImg = "none";

            //create new card object
            var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);



            hands[playerNum][cardInst] = dealtCard;
            console.log("blah");
        }
    }
    console.log(hands);
}

function card(id,name,text,img){
    this.id = id;
    this.name = name;
    this.text = text;
    this.img = img;
}

任何帮助将不胜感激!

ž

1 个答案:

答案 0 :(得分:0)

两件事;手[playerNum]没有初始化,功能卡()没有回报 尝试初始化内部对象(使用关键的playerNum),然后再添加它;

function dealCards() {
    console.log("Dealing Cards\r\n");
    var numPlayers = 3;
    var curCard;

    for (i=0;i<5;i++) {
        var cardInst = i.toString();

        for (pn =0;pn<numPlayers;pn++){
            // get current value for pn and store it
            var playerNum = pn.toString();

            //get card off of game deck
            curCard = gameDeck.shift();

            //add card info to variables
            var dcardID = baseDeck[curCard].id;
            var dcardName = baseDeck[curCard].name;
            var dcardText = baseDeck[curCard].text;
            var dcardImg = "none";

            //create new card object
            var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);

            if (hands[playerNum] === undefined){
                hands[playerNum] = {};
            }



            hands[playerNum][cardInst] = dealtCard;
            console.log("blah");
        }
    }
    console.log(hands);
}
function card(id,name,text,img){
    this.id = id;
    this.name = name;
    this.text = text;
    this.img = img;
    return this;
}

另外,我个人不喜欢使用&#34;这个点属性&#34;的构造函数样式。返回一个新对象是一个更好的习惯,所以你永远不会意外地捕获当前这个&#39;这个&#39;上下文。

function card(id,name,text,img){
    return {
       id: id,
       name: name,
       text: text,
       img: img
    }
}