JavaScript对象键未分配给变量?
我试图将boxSpaces[0]的值赋给函数decarewinner中的变量result,但赋值没有进行JavaScript对象键未分配给变量?,javascript,tic-tac-toe,Javascript,Tic Tac Toe,我试图将boxSpaces[0]的值赋给函数decarewinner中的变量result,但赋值没有进行 function assignSpace(mark, box) { const image = document.createElement("img"); const src = mark === "x" ? X_IMAGE_URL : O_IMAGE_URL; image.src = src; box.appendChild(image); box
function assignSpace(mark, box) {
const image = document.createElement("img");
const src = mark === "x" ? X_IMAGE_URL : O_IMAGE_URL;
image.src = src;
box.appendChild(image);
box.removeEventListener("click", changeToX);
const id = parseInt(box.dataset.id);
boxSpaces[id] = mark;
}
function isGameOver() {
const length = emptyBoxes.length;
if (length <= 0) {
declareWinner();
return true;
} else {
false;
}
}
function declareWinner() {
const result = "";
if (boxSpaces["0"] === boxSpaces["1"] && boxSpaces["1"] === boxSpaces["2"]) {
result = boxSpaces["0"];
console.log(result);
}
return result;
}
您正在使用const声明一个变量,并尝试更新它,这是不可能的。将其更改为let或var
检查控制台,您在那里有一个错误:TypeError:对const'result'的赋值无效
function declareWinner() {
let result = ""; //change it to let or var
if (boxSpaces["0"] === boxSpaces["1"] && boxSpaces["1"] === boxSpaces["2"]) {
result = boxSpaces["0"];
console.log(result);
}
return result;
}