1
中的變量我打電話給方法bullet.fire();在這種方法中,我有this.i ++,唯一的麻煩是this.i不會在每次迭代時更新,爲什麼?在一個循環中更新方法調用
function projectile(){
this.i = 20;
}
projectile.prototype.fire = function(){
this.i++;
//shows value of this.i in a div
document.getElementById("info").innerHTML = this.i;
}
在循環
if(typeof(bullet) == "undefined"){
var bullet = new projectile(1);
}
bullet.fire();
顯示完整循環代碼 –
我複製並運行代碼(不'的document.getElementById(「信息」)的innerHTML = this.i'和它的工作如預期 –
它更新正常,它只是一次全部更新,所以你不會看到它更新。在循環中放置一個'alert(bullet.i)';你會看到它工作正常。 –