我为“ hang子手”游戏编写了代码来练习JS编码(目前非常简单)。我忘了初始化变量'num'...我只是写了num = 2而...它可以工作,但是我不明白如何?
num = 2
document.addEventListener('click', guesser.guessLetter);
const guesser = (() => {
return {
guessLetter: (e) => {
let picture = document.querySelector('.picture')
console.log(e.target)
guess = e.path[0]
if (guess.className === 'letter') {
if (guess.id === 'h' || guess.id === 'a' || guess.id === 'n' || guess.id === 'g' || guess.id === 'm') {
let newLetter = document.querySelectorAll(`#${guess.id}1`);
let usedLetter = document.getElementById(`${guess.id}`)
newLetter.forEach(element => {
element.src = `./static/pic/chalk-alphabet/${guess.id}.png`
})
usedLetter.src = `./static/pic/chalk-alphabet/blank.png`
} else {
picture.src = `./static/pic/hangman${num}.jpg`
if (num < 10) {
return num = num + 1
} else {
return num = 10
}
}
}
}
}
}
)()