纸牌游戏,未定义错误
Card Game, Undefined Error
试图通过调用我的纸牌游戏开始,但几乎在第一行出现错误,调用是
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;
}
}
}
};
};
目前,deck函数是CardGame函数的局部变量,因此外部代码无法访问它。尝试像这样使用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++;
}
}
}
};
相关文章:
- webRTC错误未定义配置
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- Replace()产生“;未捕获的类型错误:未定义的不是函数“;当与零一起使用时
- 错误-未定义easeInBounce
- 未捕获的引用错误:未定义<映射键>
- 未捕获的类型错误:未定义不是上的函数.GetElementByID
- Javascript错误:未捕获引用错误:未定义编辑
- javascript onchange取消绑定错误|未定义
- requirejs优化器错误:未定义窗口
- 未捕获引用错误:未定义PrintThisPage
- JSHint 错误:未定义我的应用程序 (W117)
- 当我编写脚本时,它显示引用错误:未定义宽度
- socket.io - 引用错误:未定义 io
- 反应.js:未捕获类型错误:未定义不是一个函数
- 出现“未捕获的类型错误:未定义不是函数”控制台错误