我正在尝试为Hangman游戏白衣对象编写代码,但我遇到了一个大问题;我写了一个类,我想在其他类的方法中使用它。
代码是:
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!");
}
},
};
当我运行我的程序时,我有:
TypeError:this.gameWord未定义
第87行:for(i = 0; i&lt; this.gameWord.length; i ++)“。我有”这个“ 在gameWord之前。
为什么会出错?
答案 0 :(得分:0)
尝试将其设置为:
this.setAttribute("gameWord","");
然后根据需要修改