尝试通过拨打我的纸牌游戏开始,但几乎在第一行收到错误,呼叫是
var i,j, myDeck = new CardGame.Deck(game),hands = [];
在Chrome中调试时获取Type Error: undefined is not a function.
。我很确定我已经完成了其余的代码,因为我正在使用我编写的另一个程序的部分,这里我只是将代码作为模块完成,尝试将其链接到其他JS文件。
以下是包含CardGame.Deck
"use strict";
var CardGame = function() {
this.Deck = function(o) {
var length = o;
var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
this.deck = new Array(length*52);
var i, j, k;
var index = 0;
for (k = 0; k < length; k++) {
for (i = 0; i < 4; i++) {
for (j = 0; j < 13; j++) {
this.deck[i*ranks.length + j] = new Card(ranks[j], suits[i], index);
index++;
}
}
}
};
var Card = function(suits, ranks, id) {
var strRank, strSuit, theRank, theSuit;
strRank = String(ranks);
strSuit = String(suits);
var theRank = function getRank() {
switch (strRank) {
case "A" :
theRank = "Ace";
break;
case "2" :
theRank = "Two";
break;
case "3" :
theRank = "Three";
break;
case "4" :
theRank = "Four";
break;
case "5" :
theRank = "Five";
break;
case "6" :
theRank = "Six";
break;
case "7" :
theRank = "Seven";
break;
case "8" :
theRank = "Eight";
break;
case "9" :
theRank = "Nine";
break;
case "10" :
theRank = "Ten";
break;
case "J" :
theRank = "Jack";
break;
case "Q" :
theRank = "Queen";
break;
case "K" :
theRank = "King";
break;
default :
theRank = null;
break;
}
};
var theSuit = function getSuit() {
switch (strSuit) {
case "C" :
theSuit = "Clubs";
break;
case "D" :
theSuit = "Diamonds";
break;
case "H" :
theSuit = "Hearts";
break;
case "S" :
theSuit = "Spades";
break;
default :
theSuit = null;
break;
}
};
return theRank + " of " + theSuit;
};
var Pile = function() {
function Pile(p) {
if (p.length < 0) {
var pile = new Array();
} else {
var pile = new Array(p);
}
}
function draw(n) {
return this.deck.slice(0, n);
}
function drawCard() {
return this.deck.shift();
}
function add(o) {
this.deck.unshift(o);
}
function size() {
return this.deck.length;
}
function get(i) {
return this.deck.splice(i, 1);
}
function peek(i) {
var Card;
Card = this.deck(i);
return Card;
}
function shuffle() {
var i, j, temp;
var n = 10;
for (i = 0; i < n; i++) {
for (j = 0; j < this.deck.length; j++) {
k = Math.floor(Math.random() * this.deck.length);
temp = this.deck[j];
this.deck[j] = this.deck[k];
this.deck[k] = temp;
}
}
}
};
};
答案 0 :(得分:0)
目前,deck功能是Card Game功能的局部变量,因此外部代码无法访问它。请尝试使用this
关键字。
var CardGame = function() {
this.Deck = function(o){
var length = o;
var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
this.deck = new Array(length*52);
var i, j, k;
var index = 0;
for (k = 0; k < length; k++) {
for (i = 0; i < 4; i++) {
for (j = 0; j < 13; j++) {
this.deck[i*ranks.length + j] = new Card(ranks[j], suits[i], index);
index++;
}
}
}
};