我试图在不使用for循环的情况下创建一副牌。我收到错误“ Uncaught SyntaxError:第189行的意外令牌”第189行是“创建卡”的行
var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards: for (var i = 0; i < suit.length; i++){
for (var j = 0; j < name.length; j++){
deck.cards.push(card(name[j], suit[i]))
}
},
draw: function (player){
randnumber = Math.floor((Math.random() * cards.length))
player.push[cards[randnumber]];
}
};
答案 0 :(得分:3)
将cretaecards设置为匿名函数:
createcards: function(){
for (var i = 0; i < deck.suit.length; i++){
for (var j = 0; j < deck.name.length; j++){
deck.cards.push(card(deck.name[j], deck.suit[i]))
}
}
},
答案 1 :(得分:1)
您必须在文字对象中定义一个函数。
var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards: function() {
for (var i = 0; i < suit.length; i++) {
for (var j = 0; j < name.length; j++) {
deck.cards.push(card(name[j], suit[i]))
}
}
}
},
答案 2 :(得分:0)
您应该将createcards分配给匿名函数,如下所示:
createcards: function () {
for (var i = 0; i < suit.length; i++) {
for (var j = 0; j < name.length; j++) {
deck.cards.push(card(name[j], suit[i]));
}
}
}
答案 3 :(得分:0)
使用一个函数,如下所示:
var deck = {
suit: suit = ["Diamond", "Heart", "Club", "Spade"],
name: name = ["Nine", "Ten", "Jack", "Queen", "King", "Ace"],
cards: cards = [],
createcards:
function() {
for (var i = 0; i < suit.length; i++){
for (var j = 0; j < name.length; j++){
deck.cards.push(card(name[j], suit[i]))
}
}
},
draw: function (player){
randnumber = Math.floor((Math.random() * cards.length))
player.push[cards[randnumber]];
}
};
您可以像这样调用Object中的函数:
deck.createcards();
但我建议您查看以下行:
deck.cards.push(card(name[j], suit[i]))
因为这里没有功能卡......