有人可以用例子解释上面的返回内容。当您将其更改为 ( a === b ===C) 时会发生什么。一直试图在 reactjs.org 中更好地理解该逻辑,虽然运作良好,但我仍然不清楚该逻辑是如何工作的
从 React 教程复制的示例代码
`
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
//alert(lines.length);
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
//alert(squares[a]);
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null; …Run Code Online (Sandbox Code Playgroud)