我正在尝试创建一副纸牌,但是运行deckOfCards时出现标题错误。

function deckOfCards() {
    const names = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
    const suits = ['Hearts','Diamonds','Spades','Clubs'];
    var deck = new Array();

    function card(name, suits) {
        this.name = name;
        this.suits = suits;
    }

    function createDeck() {
        suits.forEach(function (names) {
            names.forEach(function (type) {
                deck.push(new card(type, weight));
            });
        });
    }

    createDeck();
}

最佳答案

好的,我想这就是您要的。我将deckOfCards更改为对象。然后解决了createDeck变量问题。


Javascript:jsFiddler

var deckOfCards = {
    names : ['A','2','3','4','5','6','7','8','9','10','J','Q','K'],
    suits : ['Hearts','Diamonds','Spades','Clubs'],
     deck : []
}

function card(name, suits){
    this.name = name;
    this.suits = suits;
}

function createDeck(){
    deckOfCards.suits.forEach(function (suit) {
        deckOfCards.names.forEach(function (name) {
           deckOfCards.deck.push(new card(name, suit));
        });
    });
}

createDeck();
console.log(deckOfCards.deck);

09-13 01:14