简体   繁体   中英

Square brackets after arguments list in JavaScript/TypeScript

Can anyone explain the meaning behind the "[0]" after the arguments list here?

let value = this.recurseMinimax(board, !player) [0] ;

function:

 recurseMinimax(board: boolean[][], player: boolean): any {
    this.numNodes++;
    let winner = this.getWinner(board);
    if (winner != null) {
      switch (winner) {
        case 1:
          return [1, board];
        case 0:
          return [-1, board];
        case -1:
          return [0, board];
      }
    } else {
      let nextVal = null;
      let nextBoard = null;

      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          if (board[i][j] == null) {
            board[i][j] = player;
            let value = this.recurseMinimax(board, !player)[0];
            if ((player && (nextVal == null || value > nextVal)) || (!player && (nextVal == null || value < nextVal))) {
              nextBoard = board.map(function (arr) {
                return arr.slice();
              });
              nextVal = value;
            }
            board[i][j] = null;
          }
        }
      }
      return [nextVal, nextBoard];
    }
  }

And the "[1]" here:

return this.recurseMinimax(board, true) [1] ;

function:

minimaxMove(board: boolean[][]): any {
    this.numNodes = 0;
    return this.recurseMinimax(board, true)[1];
  }

It Means the index of the Array (that is returning from your functions) are you accessing ... be carefully to do this without do a check maybe before, for example:

  minimaxMove(board: boolean[][]): any {
        this.numNodes = 0;
        return this.recurseMinimax(board, true) && this.recurseMinimax(board, true).lenght>=1 ? this.recurseMinimax(board, true)[1] : {} ;
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM