我有一个函数private:
int numbers;
int coffee;
如下:
currentPlayer
我正在尝试在reducer中使用此函数,如下所示:
function currentPlayer(players) {
return players.findIndex(player => player.current) || 0;
}
运行此代码时,出现以下错误:
ReferenceError:未定义currentPlayer
如果我按如下方式更改变量名称,则代码有效:
export function players(players = [], action) {
players = players.slice();
switch (action.type) {
...
case DELETE_PLAYER:
const currentPlayer = currentPlayer(players);
if (currentPlayer === action.index) {
players = switchNextPlayer(players, currentPlayer);
}
players.splice(action.index, 1);
}
return players;
}
我认为变量const currentPlayerIndex = currentPlayer(players);
将在reducer函数中作用域。这是怎么回事?
答案 0 :(得分:1)
在初始化表达式之前,const
声明被解释为。因此,您的本地范围首先获得其自己的currentPlayer
符号,然后评估初始化表达式。因此,在初始化表达式中,currentPlayer
引用新声明的本地符号,而不是函数。