Javascript构造函数不起作用

时间:2013-08-28 17:55:21

标签: javascript arrays constructor

我有一些数组和一个简单的构造函数,但不知怎的,我无法让它工作。

var suit = ["ruutu", "risti", "\u00E4rtu", "poti"],
    type = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "soldat", "emand", "kuningas", "\u00E4ss"],
    pnt = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11],
    card = {}, // 3 properties: suit, type, pnt)
    Cards = [];

Cards = makeDeck(card, suit, type, pnt, Cards);
card = new NewCard(Cards);

function makeDeck(card, suit, type, pnt, Cards) {
    "use strict";
    var i,
        j;
    for (i = 0; i < 4; i = i + 1) {
        for (j = 0; j < 13; j = j + 1) {
            card = {
                suit : suit[i],
                type : type[j],
                pnt : pnt[j]
            };
            Cards.push(card);
        }
    }
    return Cards;
}

function NewCard(Cards) {
    "use strict";
    var i = Math.floor(Math.random() * 52);
    this.suit = Cards[i].suit;
    this.type = Cards[i].type;
    this.pnt = Cards[i].pnt;
}

我可以在Firebug中看到,当this.suit和this.type获取它们的值时,this.pnt仍未定义。为什么?它与数据类型(数字与字符串)有关吗?

提前致谢!

0 个答案:

没有答案