我想插入一個值到數組中,但是當我嘗試插入值時,它給了我Connot set屬性'0'的未定義。但顯然我已經定義了陣列。未捕獲TypeError:無法設置未定義的屬性'0'
錯誤行:
this.enemyMinions[i] = new EnemyCombatMinion(new Minion(enemyMinionInfo[0], 0, enemyMinionInfo[1], enemyMinionInfo[2], enemyMinionInfo[3], 0, 0, 0, 0, 0, 0));
這是整個代碼:
function Combat() {
this.enemyMinions = [];
this.playerMinions = [];
this.currentEnemyMinion = null;
this.currentPlayerMinion = null;
}
Combat.prototype.initialize = function() {
var o = 0;
for(var i = 0; i < partySlots.length; i++) {
if(partySlots[i]) {
this.playerMinions[o] = PlayerCombatMinion(partySlots[i]);
o++;
}
}
this.currentPlayerMinion = this.playerMinions[0];
$.ajax({
url: "./api/generateEnemyCombatMinions.php",
cache: false
})
.done(function(html) {
var response = html.split(":::");
for(var i = 0; i < response.length; i++) {
var enemyMinionInfo = response[i].split("::");
this.enemyMinions[i] = new EnemyCombatMinion(new Minion(enemyMinionInfo[0], 0, enemyMinionInfo[1], enemyMinionInfo[2], enemyMinionInfo[3], 0, 0, 0, 0, 0, 0));
}
alert(this.enemyMinions.length);
});
}
正如你看到的,我宣佈this.enemyMinons在類的構造文字,並試圖定義初始化方法中的值。
是'var enemyMinionInfo = response [i] .split(「::」);'返回一個數組? –
你的問題是'this'不是你想象的那樣。 –
檢查「this」出來,它可能不是你認爲 – MJB