Javascript 此视图类方法display()不工作?
在这个q中,我没有添加模型和事件类,而是添加了视图和控制器类,这是一个tic tac toe游戏q。 tic tac toe游戏的视图类Javascript 此视图类方法display()不工作?,javascript,model-view-controller,tic-tac-toe,javascriptmvc,Javascript,Model View Controller,Tic Tac Toe,Javascriptmvc,在这个q中,我没有添加模型和事件类,而是添加了视图和控制器类,这是一个tic tac toe游戏q。 tic tac toe游戏的视图类 class View { constructor() { this.playEvent = new Event(); } display() { const board = document.createElement('div'); board.className = 'board'; this.cells = Array(9).f
class View {
constructor() {
this.playEvent = new Event();
}
display() {
const board = document.createElement('div');
board.className = 'board';
this.cells = Array(9).fill().map((_, i) => {
const cell = document.createElement('div');
cell.className = 'cell';
cell.addEventListener('click', () => {
this.playEvent.triggerListener(i);
});
board.appendChild(cell);
return cell;
});
this.message = document.createElement('div');
this.message.className = 'message';
document.body.appendChild(board);
document.body.appendChild(this.message);
}
victory(winner) {
this.message.innerHTML = `${winner} wins!`;
}
draw() {
this.message.innerHTML = "It's a draw!";
}
}
此显示器不工作我的控制器类不工作
class Controller {
constructor(model, view) {
this.model = new Model();
this.view = new View();
接下来的3行是模型和视图的连接
this.view.playEvent.addListener(moveIndex => { this.model.gameStart(moveIndex); });
this.model.gameWinnerEvent.addListeners(winner => { this.view.gamewinner(winner); });
this.model.gameDrawEvent.addListeners(() => { this.view.gameDraw(); });
}
run(){
this.view.display()
}
}
这是我运行整个程序的地方
const app = new Controller()
app.run()
您可能应该这样定义显示方法:
function display() {
// your code
}
通过像您那样定义函数,JS理解它必须在类的定义上执行(就像构造函数一样)display方法仅在类中。你能再检查一遍代码吗?请注意我的缩进。