问题描述
我正在做一个 HTML、CSS、JSS tictactoe 项目,但我遇到了一个错误,当我尝试使用console.log 在我的文本编辑器中:
“未捕获的引用错误:topLeft 未定义”
if (topLeft && topLeft === topMiddle && topLeft === topRight) {
我的代码:
from . import trn_db as td
我所做的代码似乎与视频相同,所以我不确定为什么会收到此错误。
参考文献
- GitHub
教程中的人有自己的 GitHub,代码为 enter link description here
- 优酷
这将是我目前的流程 enter link description here
我之前发布过类似的问题,这是我第一次解决问题。有点担心它可能会再次关闭,所以如果请让我知道我做错了问题的哪一部分。
解决方法
您的问题与block scoping有关。你在这个块中声明你的变量:
const checkGameStatus = () => {
const topLeft = cellDivs[0].classList[2];
[...]
}
但是随后您试图访问块外的变量,因为它们不存在。
相反,初始化块外的变量,然后赋值:
let topLeft;
const checkGameStatus = () => {
topLeft = cellDivs[0].classList[2];
[...]
}
checkGameStatus()
if (topLeft && topLeft === topMiddle && topLeft === topRight) {
console.log(topLeft);
}