3

ActionScript 3は、デフォルトで参照によって配列を渡します。私は新人の間違いを犯しているに違いありません。これが私のコードのスナップショットです:

private function testFunc():void {
    var testArray:Array=new Array();
    myFunction(testArray);
    trace(testArray); // []; length=0
}

private function myFunction(tArray:Array):void {
    tArray = myOtherFunction();
    trace(tArray); // 0, 1, 2; length=3
}

private function myOtherFunction():Array {
    var x:Array=new Array;
    for (var i:int=0; i<3; i++)
       x[i]=i;
    return x;
}

tArray私はそれが正しいことを見ることができますが、testArray常に空です。testArray等しくする方法はありtArrayますか?前もって感謝します。

http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000049.html

アップデート:

その価値について、私は次の変更(ハック)が機能することを発見しました:

private function myFunction(tArray:Array):void {
    var Z:Array=new Array;
    Z = myOtherFunction();
    for (var i:int=0; i<Z.length; i++)
        tArray[i]=Z[i];
}

ただし、Georgiiのソリューションはより優れた設計です。

4

3 に答える 3

4

testArray をパラメーターとして myFunction に渡すと、その参照がコピーされ、ローカル参照 tArray に割り当てられます。これにより、myFunction tArray の内部は testArray と同じオブジェクトを指しますが、実際には別の参照になります。そのため、tArray の参照を変更しても、testArray 自体は変更されません。

private function testFunc():void {
    var testArray:Array=new Array(); 
    // testArray is a local variable, 
    // its value is a reference to an Array object
    myFunction(testArray);
    trace(testArray); // []; length=0
}

private function myFunction(tArray:Array):void {
    // tArray is a local variable, which value equals to testArray
    tArray = myOtherFunction(); 
    // now you changed it and tArray no longer points to the old array
    // however testArray inside of testFunc stays the same
    trace(tArray); // 0, 1, 2; length=3
}

おそらくあなたが望むのは:

private function testFunc():void {
    var testArray:Array=new Array();
    testArray = myFunction(testArray);
    trace(testArray); // 0, 1, 2; length=3
}

private function myFunction(tArray:Array):Array {
    // do what you want with tArray
    tArray = myOtherFunction();
    trace(tArray); // 0, 1, 2; length=3
    // return new value of the tArray
    return tArray;
}

private function myOtherFunction():Array {
    var x:Array=new Array;
    for (var i:int=0; i<3; i++)
       x[i]=i;
    return x;
}
于 2013-01-30T03:45:34.437 に答える
2

基本的に何が起こっているのかというと、配列を関数に渡し、それが(関数内で)その配列へのローカル参照を作成します。

つまり、先に進んで新しく作成した配列をローカル変数(このtArray場合)に割り当てると、代わりにその配列が使用され、元の配列は元のインスタンスを保持します。

これは、たとえばスプライトを使用するすべてのオブジェクトで同じです。

var sprite:Sprite = new Sprite();

// We set the original Sprite's x to 10.
sprite.x = 10;

function mutilate(subject:Sprite):void
{
    // Notice here how we are making a new instance. This won't assign a new
    // instance to the "sprite" property that we've passed here, but rather
    // a new instance to the local variable "subject".
    subject = new Sprite();

    // And here we set the x of the new instance to 20.
    subject.x = 20;
}


mutilate(sprite);
trace(sprite.x); // Outputs 10, though you may have expected 20.
于 2013-01-30T03:51:13.550 に答える
0

myOtherFunction() の戻り値を取得して反復し、要素ごとに tArray.push() を呼び出します。現在行っていることは、新しく作成されたオブジェクトへのローカル参照を変更することだけです。

元の配列に要素を追加する必要がありますが、割り当て時に参照も失われます。

于 2013-01-30T04:01:48.750 に答える