簡體   English   中英

我正在嘗試理解React TicTacToe教程。 他們的calculateWinner幫助程序功能如何運行?

[英]I'm trying to make sense of the React TicTacToe tutorial. How does their calculateWinner helper function operate?

首先,這不是對React教程功能computeWinner(squares)不了解的偽裝

查看https://reactjs.org/tutorial/tutorial.html#declaring-a-winner

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],
  ];


  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];
    }

  }
  return null;
}

const[a, b, c] = lines[i]; 做? 什么是方格[a],方格[b]和方格[c]? 我知道lines數組包含導致獲勝的所有正方形位置組合,但是我不明白它們是如何定義和比較的,並且本教程實際上沒有提供任何解釋,只是“這是一個輔助函數這樣就可以了。”

lines[i]表示三個值( [0, 1, 2][3, 4, 5]等)的八個組合之一。 const[a, b, c] = lines[i]; 將這三個值分配給變量abc 這使得下面的if語句中的條件更加簡潔,因為這三個變量用作squares數組的索引。

const[a, b, c] = lines[i];

相當於

const a = lines[i][0];
const b = lines[i][1];
const c = lines[i][2];

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

井字代碼https://codepen.io/gaearon/pen/LyyXgK?editors=0010

squares是來自網格的squares slice

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM