正如其他用户所说,在循环期间修改数组的大小是一个坏主意。尝试:
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);
现场演示
编辑:抱歉没有注意到您更改了代码。更新:
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);
现场演示