Javascript TypeError:此.gameWord未定义
我正试图为Hangman游戏whit objects编写代码,但我有一个大问题;我写了一个类,我想在其中使用另一个类的方法 代码是:Javascript TypeError:此.gameWord未定义,javascript,Javascript,我正试图为Hangman游戏whit objects编写代码,但我有一个大问题;我写了一个类,我想在其中使用另一个类的方法 代码是: var Game = { // All game rightGuess : "", wrongGuess : "", gameWord : DisplaySecretWord.selectWord(), gameSecret : DisplaySecretWord.getSecretWord(), isLette
var Game = {
// All game
rightGuess : "",
wrongGuess : "",
gameWord : DisplaySecretWord.selectWord(),
gameSecret : DisplaySecretWord.getSecretWord(),
isLetterInWord : function(letter){
var isRightGuess = false;
for(i = 0; i < this.gameWord.length; i++)
{
if(letter == this.gameWord.substring(i, i + 1))
{
this.rightGuess += letter;
this.gameSecret = this.gameSecret.substring(0, i) + letter + this.gameSecret.substring(i + 1, this.gameSecret.length + 1);
DisplaySecretWord.dispayWord(this.gameSecret);
var el = document.getElementById("right");
if (el != null || el != undefined ){
el.innerHTML = this.rightGuess;
}
isRightGuess = true;
this.win();
}
}
if(!isRightGuess)
{
this.wrongGuess += letter;
var e = document.getElementById("wrong");
if (e != null || e != undefined ){
e.innerHTML = this.wrongGuess;
}
DisplayBoard.displayPic(this.wrongGuess);
this.lose();
}
},
win : function(){
if(this.gameSecret == this.gameWord){
alert("Good job!!! You win! The word was " + this.gameWord);
}
},
lose : function(){
if( this.wrongGuess.length == maxWrong){
alert("Sorry but you lose!!! The word was " + this.gameWord + " .Click 'New Game' to play again!");
}
},
};
var游戏={
//全场比赛
对:“,
猜错了:“,
gameWord:DisplaySecretWord.selectWord(),
gameSecret:DisplaySecretWord.getSecretWord(),
isLetterInWord:函数(字母){
var isRightGuess=false;
对于(i=0;i
当我运行我的程序时,我有:
TypeError:此.gameWord未定义
第87行:for(i=0;ithis.setAttribute("gameWord","");
然后根据需要修改它它是如何初始化/调用的?如果您尝试
Game.gameWord
?我正在尝试Game.isLetterInWord()是您的方法DisplaySecretWord.selectWord()
realy return some word?将变量gameWord
放在控制台console.log(this.gameWord)
;是的,显示secretword.selectWord(),我正在检查它