在這段代碼中,我添加了一個「下注」的動作,並將其id作爲參數傳遞給一個函數。但是當我稍後調用這個箭頭函數時,this.undoBet
的參數等於this.local_bets[this.local_bets.length].bet_id
--最後一個在循環內傳遞的bet_id
。Typescript,箭頭函數內部的函數參數
如何使它在每個箭頭函數內部,this.undoBet
將保留在該循環中分配給它的bet_id
?
for (var k in this.local_bets) {
var bet = this.local_bets[k];
if (bet.status == BetStatus.accepted) {
// Here bet_id is correct for every "bet" variable
this.addUndo("undo_bet",() => {
// When calling this later, bet_id equals to one that belongs to the last bet inside this.local_bets
this.undoBet(bet.bet_id);
});
}
}
它的工作,謝謝! – ohyou