如何将结果从playerElementChoice()方法传递给startGame()
class Game {
constructor() {
this.options = document.querySelectorAll('.options');
this.options.forEach(item => {
item.addEventListener('click', this.playerElementChoice);
});
document.querySelector('button.start').addEventListener('click', this.startGame.bind(this));
}
playerElementChoice() {
const name = this.firstElementChild.className;
return name;
}
startGame() {
console.log(this.playerElementChoice());
}
}
尝试在方法startGame()中调用方法playerElementChoice()时出现错误:
无法从Game.startGame(Game.js:28)的Game.playerElementChoice(Game.js:17)读取未定义的属性“ className”。
我在做什么错了?