Javascript I';我用vanilla JS创建了一个刽子手游戏,我偶然发现了这个错误,我可以';I don’我想不出原因
我正在用vanilla JS创建一个刽子手游戏,我无意中发现了这个错误,我无法找出原因 错误表示“this.guessedLetters”在“getStatus prototype”中未定义。我真的不明白为什么会这样 代码如下:Javascript I';我用vanilla JS创建了一个刽子手游戏,我偶然发现了这个错误,我可以';I don’我想不出原因,javascript,Javascript,我正在用vanilla JS创建一个刽子手游戏,我无意中发现了这个错误,我无法找出原因 错误表示“this.guessedLetters”在“getStatus prototype”中未定义。我真的不明白为什么会这样 代码如下: const Hangman = function (word, remainingGuesses) { this.word = word.toLowerCase().split('') this.remainingGuesses = remainingG
const Hangman = function (word, remainingGuesses) {
this.word = word.toLowerCase().split('')
this.remainingGuesses = remainingGuesses
this.guessedLetters = []
this.status = 'playing'
}
Hangman.prototype.getStatus = function () {
let finished = true
this.word.forEach(function(item){
if(this.guessedLetters.includes(item))
finished = false
})
if((this.remainingGuesses === 0 || this.remainingGuesses < 0) && finished === true ){
this.status = 'failed'
}
else if(!finished){
this.status = 'finished'
}
else{
this.status = 'playing'
}
}
Hangman.prototype.getPuzzle = function () {
let puzzle = ''
this.word.forEach((letter) => {
if (this.guessedLetters.includes(letter)) {
puzzle += letter
} else {
puzzle += '*'
}
})
return puzzle
}
Hangman.prototype.makeGuess = function (guess) {
guess = guess.toLowerCase()
const isUnique = !this.guessedLetters.includes(guess)
const isBadGuess = !this.word.includes(guess)
if (isUnique) {
this.guessedLetters.push(guess)
}
if (isUnique && isBadGuess) {
this.remainingGuesses--
}
}
const game1 = new Hangman('Cat', 2)
console.log(game1.getPuzzle())
console.log(game1.remainingGuesses)
document.querySelector('#godzillaX').textContent = game1.getPuzzle()
document.querySelector('#godzillaY').textContent = game1.remainingGuesses
console.log(game1.status)
window.addEventListener('keypress', function (e) {
const guess = e.key
game1.makeGuess(guess)
console.log(game1.getPuzzle())
console.log(game1.remainingGuesses)
document.querySelector('#godzillaX').textContent = game1.getPuzzle()
document.querySelector('#godzillaY').textContent = game1.remainingGuesses
game1.getStatus()
console.log(game1.status)
})
const Hangman=函数(单词,剩余猜测){
this.word=word.toLowerCase().split(“”)
this.remainingGuesses=remainingGuesses
this.guessedLetters=[]
this.status='playing'
}
Hangman.prototype.getStatus=函数(){
让完成=真
this.word.forEach(函数(项){
if(本.猜测字母.包括(项目))
完成=错误
})
if((this.remainingGuesses==0 | | this.remainingGuesses<0)&&finished==true){
this.status='失败'
}
否则,如果(!完成){
this.status='finished'
}
否则{
this.status='playing'
}
}
Hangman.prototype.getPuzzle=函数(){
让益智=“”
this.word.forEach((字母)=>{
if(本.猜测字母.包括(字母)){
拼图+=字母
}否则{
拼图+='*'
}
})
返回谜题
}
Hangman.prototype.makeGuess=函数(猜测){
guess=guess.toLowerCase()
const isUnique=!this.guessedLetters.includes(guess)
const isBadGuess=!this.word.includes(guess)
如果(是唯一的){
这个。猜字母。推(猜)
}
if(isUnique&&isBadGuess){
这是我的猜测--
}
}
const game1=新刽子手('Cat',2)
console.log(game1.getPuzzle())
console.log(game1.remainingGuesses)
document.querySelector('#godzillaX').textContent=game1.getPuzzle()
document.querySelector(“#哥斯拉”).textContent=game1.remaingGuesses
console.log(game1.status)
window.addEventListener('keypress',函数(e){
常数猜测=e.key
游戏1.猜(猜)
console.log(game1.getPuzzle())
console.log(game1.remainingGuesses)
document.querySelector('#godzillaX').textContent=game1.getPuzzle()
document.querySelector(“#哥斯拉”).textContent=game1.remaingGuesses
game1.getStatus()
console.log(game1.status)
})
此错误是因为此
指向函数(项){
,而不是Hangman
对象实例
我认为有两种可能的解决办法:
Hangman.prototype.getStatus=函数(){
让完成=真
const gl=this.guessedLetters
this.word.forEach(函数(项){
如果(总帐包括(项目))
完成=错误
})
此将指向父项:
Hangman.prototype.getStatus=函数(){
让完成=真
this.word.forEach(项=>{
if(本.猜测字母.包括(项目))
完成=错误
})
:“写一个标题,总结具体问题。这个标题是潜在的回答者会看到的第一件事,如果你的标题不感兴趣,他们就不会读其余的。”//它说要添加更多细节,所以请忽略下面的文本//……”-不要欺骗系统。这些检查/规则是有原因的。你的这个。猜字母指向函数(项){
而不是父项。执行var self=this
并用self
替换随处可见的this
。或使用胖箭头函数。范围问题使用箭头函数解决了问题。谢谢!