实例地址: https://codepen.io/gaearon/pe...
// 使用forEach就不行
lines.forEach(item=>{
const [a,b,c] = item
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]){
console.log(squares[a])
return squares[a]
}
})
// 官方示例用的for循环
// for (let i = 0; i < lines.length; i++) {
// const [a, b, c] = lines[i];
// if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
// return squares[a];
// }
// }
使用for循环来判断胜者条件就可以成功,使用forEach就不行,打印forEach return有值
const winner = calculateWinner(this.state.squares)
let status
if (winner){
status = 'Winner:'+ winner
} else {
status = 'Next player: '+(this.state.xIsNext?'X':'O')
}
但是后面的winner一直拿不到这个return的值,一直为null.是forEach中return有问题?
forEach不能返回值而且不能中断