【发布时间】:2020-06-29 21:54:21
【问题描述】:
我知道这是一个奇怪的请求,但我不完全理解为什么我的代码从句法的角度来看是有效的。需要明确的是,它确实按预期工作,完全是我自己的代码,但我对我为达到我所在的位置所做的事情有点迷茫。每个带有 `gameplay' 对象的单独函数(或函数,我不知道它叫什么)都可以忽略。
const Player = function(name, symbol, score) {
return {name, symbol, score};
}
let playerOne = new Player(document.getElementById('oneName').value, 'x', 0);
let playerTwo = new Player(document.getElementById('twoName').value, 'o', 0);
const updateNames = function() {
playerOne.name = document.getElementById('oneName').value;
playerTwo.name = document.getElementById('twoName').value;
}
const gameplay = (() => {
let turn = 'x';
const board = {
1: '',
2: '',
3: '',
4: '',
5: '',
6: '',
7: '',
8: '',
9: ''
}
const checkWinner = function() {
let winCombinations = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[7, 5, 3],
];
winCombinations.forEach(function(array) {
let x = 0;
Object.keys(board).forEach(function(key) {
if (board[key]) {
x += 1;
}
})
if (board[array[0]] === 'x' && board[array[1]] === 'x' && board[array[2]] === 'x') {
displayWinner(playerOne.name ? playerOne.name : 'Player One');
playerOne.score += 1;
document.getElementById('oneScore').innerHTML = playerOne.score;
} else if (board[array[0]] === 'o' && board[array[1]] === 'o' && board[array[2]] === 'o') {
displayWinner(playerTwo.name ? playerTwo.name : 'Player Two');
playerTwo.score += 1;
document.getElementById('twoScore').innerHTML = playerTwo.score;
} else if (x === 9) {
displayWinner(false);
};
})
}
const displayWinner = function(winner) {
const x = document.getElementById('winMessage');
x.innerHTML = winner ? `${winner} is the winner!` : 'TIE!';
x.style.display = 'flex';
x.addEventListener('click', refresh);
}
const refresh = function() {
updateNames();
const x = document.getElementById('htmlBoard');
while (x.firstChild) {
x.removeChild(x.lastChild)
}
for (i = 1; i < 10; i++) {
board[i] = '';
};
document.getElementById('winMessage').style.display = 'none';
turn = 'x';
render();
}
const restart = function() {
playerOne.score = 0;
playerTwo.score = 0;
document.getElementById('oneScore').innerHTML = playerOne.score;
document.getElementById('twoScore').innerHTML = playerTwo.score;
const x = document.getElementById('htmlBoard');
refresh();
turn = 'x';
}
const commitPlay = function() {
if (!(board[event.target.id])) {
if (turn === 'x') {
event.target.className = 'x';
event.target.style.cursor = 'not-allowed';
board[event.target.id] = 'x';
turn = 'o';
} else {
event.target.className = 'o';
event.target.style.cursor = 'not-allowed';
board[event.target.id] = 'o';
turn = 'x';
}
checkWinner();
}
}
const render = function() {
for (i = 1; i <= 9; i++) {
let tile = document.createElement('div');
tile.id = i;
document.getElementById('htmlBoard').appendChild(tile);
tile.addEventListener('click', commitPlay);
}
}
return {board, render, restart, refresh};
})(); //<----
document.getElementById('clearBoard').addEventListener('click', gameplay.refresh);
document.getElementById('newGame').addEventListener('click', gameplay.restart);
window.onload = function () {
gameplay.render();
};
我的主要问题是关于gameplay 对象/函数。它是什么,它如何影响我的代码的工作方式?此外,我在它的最后一行添加了一个箭头,因为我不明白为什么我需要在同一行上的左括号和右括号,里面什么都没有,但没有它们就行不通。
由于这是一个如此奇怪的问题并且措辞可怕,我将密切关注这个问题以澄清任何事情。提前致谢。
【问题讨论】:
-
这是一个 IIFE (developer.mozilla.org/en-US/docs/Glossary/IIFE),只是带有箭头函数而不是“常规”
function -
打开和关闭括号会立即调用您定义的函数('invoke')。该函数内部的所有内容都不再被该函数之外的任何人访问,除了它返回的内容。
标签: javascript object constructor