0

キャンバスの図形を削除する必要があります。

次のコードが書かれました-

  function deleteCircle(circle, propName) {
      for( i = 0; i < circle.length; i++) {
        circle[i].erase();      
      }
    }
   circle.prototype.erase=function() {
      delete this;
    }

このコードをi=0で実行すると、消去機能が実行されます。しかし、最初の削除の後、それはdeletecircleに戻っていません。これは単なる大まかなコードです...これを削除した後....つまり、オブジェクトが削除された後、コードはdeletecircleに戻りませんか?

4

2 に答える 2

1

As other users have said, modifying size of your array during the loop is a bad idea. Try:

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

EDIT: Sorry didn't notice you changed the code. Updated:

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

于 2012-08-16T09:24:36.273 に答える
0

Your circle object doesn't have erase method. In your case erase is separate function and won't work like this.

于 2012-08-16T09:10:01.707 に答える