正如其他用戶所說,在循環中修改數組的大小是一個壞主意。 嘗試:
var circle = new Array();
circle[0]=1;
circle[1]=2;
circle[2]=3;
circle[3]=4;
function deleteCircle(circle, propName) {
while(circle.length!==0){
erase(circle.pop());
}
}
function erase(elem){
delete elem;
}
alert(circle.length);
deleteCircle(circle,null);
alert(circle.length);
LIVE DEMO
編輯:很抱歉沒注意到你改變了代碼。更新:
function circle(){};
circle.prototype.erase = function(){
delete this;
}
var circles = [new circle, new circle, new circle];
function deleteCircle(circle, propName) {
while(circle.length!==0){
circle.pop().erase();
}
}
function circle(){}; //or however you define your class
circle.prototype.erase = function(){ //extend the class
delete this;
}
var circles = [new circle, new circle, new circle]; //your array
function deleteCircle(circle, propName) {
while(circle.length!==0){
circle.pop().erase();
}
}
alert(circles.length);
deleteCircle(circles,null);
alert(circles.length);
LIVE DEMO
我假定一個誤差在'擦除()',其防止循環持續發生。你有沒有檢查錯誤日誌? – Curt 2012-08-16 09:08:13
您正在修改循環內數組的長度 - 這是一個壞主意。 – 2012-08-16 09:08:20
加'我'是漏水的。 – elclanrs 2012-08-16 09:09:09