JavaScript/TypeScript 中参数列表后的方括号
Square brackets after arguments list in JavaScript/TypeScript
谁能解释一下参数列表后面的“[0]”背后的含义?
let value = this.recurseMinimax(board, !player)[0];
函数:
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];
}
}
这里的“[1]”:
return this.recurseMinimax(board, true)[1];
函数:
minimaxMove(board: boolean[][]): any {
this.numNodes = 0;
return this.recurseMinimax(board, true)[1];
}
这意味着您正在访问数组的索引(从您的函数返回)...请小心执行此操作,而不要事先进行检查,例如:
minimaxMove(board: boolean[][]): any {
this.numNodes = 0;
return this.recurseMinimax(board, true) && this.recurseMinimax(board, true).lenght>=1 ? this.recurseMinimax(board, true)[1] : {} ;
}
谁能解释一下参数列表后面的“[0]”背后的含义?
let value = this.recurseMinimax(board, !player)[0];
函数:
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];
}
}
这里的“[1]”:
return this.recurseMinimax(board, true)[1];
函数:
minimaxMove(board: boolean[][]): any {
this.numNodes = 0;
return this.recurseMinimax(board, true)[1];
}
这意味着您正在访问数组的索引(从您的函数返回)...请小心执行此操作,而不要事先进行检查,例如:
minimaxMove(board: boolean[][]): any {
this.numNodes = 0;
return this.recurseMinimax(board, true) && this.recurseMinimax(board, true).lenght>=1 ? this.recurseMinimax(board, true)[1] : {} ;
}