As3: How to copy an object by value

I need to have an instance of one common object in every other object. I am making changes to the values ​​of this object in each subobject that I have.

For instance. I have a tile map, and the bot moves around them in a certain order. Each bot marks tiles that it has already visited, since visit = true. But overall I don’t want to change the main card ...

I tried to create an example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    applicationComplete="complete()">
    <mx:Script>
        <![CDATA[
            private var array:Array = new Array( 1, 2, 3, 4);
            public function complete():void
            {
                trace("here " + array);
                var a:Array = array;
                a[0] = 100;
                trace("here " + array);
            }
        ]]>
    </mx:Script>
</mx:Application>

Can someone help me understand how to copy, for example, an array by value (not by reference)

+5
source share
3 answers
function clone ( arr:Array ):Array
{
    return arr.map( function( item:*, ..r ):*
        {
            return item;
        } );
}

edit:

May contain some syntax errors ...

public class MyObject
{
    private var arr:Array;
    private var bool:Boolean;

    // ...

    public function clone ():MyObject
    {
        var obj:MyObject = new MyObject();

        // clone values
        obj.arr = this.arr.slice();
        obj.bool = this.bool;

        return obj;
    }
}
+8
source

Array.slice.

var arrCopy:Array = arrOrig.slice();
+10

, poke:

, .

  • " . , ". . ActionScript , AMF ( ActionScript). AMF .

  • " , , , ..." , , , .

  • "... (.. )." . - , .

  • "[ ] ( , , )." , , . , . .

  • " , , ". , slice() concat() - , . , . , . , - - . - , .

: - ?

" - , , , ( )" , .

(Array.slice() Array.concat()) . . - (, , ), - . , , , , . , . , , .

, , . - - , .

, poke, . clone() . , , Array Dictionary, , , , , clone() , . . , : ( ); clone ( , clone).

- AMF , - , . - , -, .

var t:Array = [];
t[0] = [1, 2, 3];
t[1] = new Dictionary();
t[1]['hello'] = 'world';
t[2] = {'my': 'object'}
trace(t, t[1]['hello'], t[2]['my']); // [trace] 1,2,3,[object Dictionary],[object Object] world object
var t2:Array = clone(t);
trace(t2, t2[1]['hello'], t2[2]['my']); // [trace] 1,2,3,[object Dictionary],[object Object] world object
t[0] = [4, 5, 6];
t[1]['hello'] = 'earth';
t[2]['my'] = 'other object';
trace('modified values');  // [trace] modified values
trace(t, t[1]['hello'], t[2]['my']);  // [trace] 4,5,6,[object Dictionary],[object Object] earth other object
trace(t2, t2[1]['hello'], t2[2]['my']);  // [trace] 1,2,3,[object Dictionary],[object Object] world object

function clone(source:*):* {
    var b:ByteArray = new ByteArray();
    b.writeObject(source);
    b.position = 0;
    return(b.readObject());
}

1 . , .

, : mx.utils.ObjectUtil.

, , IExternalizable. :

public function writeExternal(output:IDataOutput):void
public function readExternal(input:IDataInput):void

, . , , . .

:

package {

    import flash.utils.IExternalizable;
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;
    import flash.net.registerClassAlias;

    public class Car implements IExternalizable {

    private var type:String;
        private var contents:Array;

        public function Car() {
            registerClassAlias("Car", Car);
        }

    public function setVars(type:String, contents:Array):void {
            this.type = type;
            this.contents = contents;
        }

    public function setType(type:String):void {
            this.type = type;
        }


        public function writeExternal(output:IDataOutput):void {
            output.writeUTF(type);
            output.writeObject(contents);
        }

    public function readExternal(input:IDataInput):void {
            type = input.readUTF();
            contents = input.readObject();
    }

        public function toString():String {
            return "[Car type = " + type + ", contents = " + contents + "]";
    }

    }

}

package {

    import flash.utils.IExternalizable;
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;
    import flash.net.registerClassAlias;

    public class Person implements IExternalizable {

    private var firstName:String;
        private var secondName:String;

        public function Person() {
            registerClassAlias("Person", Person);
        }

    public function setVars(firstName:String, secondName:String):void {
            this.firstName = firstName;
            this.secondName = secondName;
        }

        public function writeExternal(output:IDataOutput):void {
            output.writeUTF(firstName);
            output.writeUTF(secondName);
        }

    public function readExternal(input:IDataInput):void {
            firstName = input.readUTF();
            secondName = input.readUTF();
        }

        public function toString():String {
            return "[Person firstName = " + firstName + ", secondName = " + secondName + "]";
        }

    }

}

:

package {

    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    public class Serial extends Sprite {

    public function Serial() {

            var person0:Person = new Person();
            person0.setVars("John", "Doe");
            var person1:Person = new Person();
            person1.setVars("Jane", "Doe");
            var car0:Car = new Car();
            car0.setVars("Ford", [person0, person1]);

            var person2:Person = new Person();
            person2.setVars("Joe", "Bloggs");
            var car1:Car = new Car();
            car1.setVars("Vauxhall", [person2]);

            var street:Array = [car0, car1];
            trace("street = " + street); // [trace] street = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]

            var street2:Array = clone(street);
            trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]

            person0.setVars("Max", "Headroom");
            person1.setVars("Simon", "Le Bon");
            car0.setType("Mini");

            person2.setVars("Harry", "Wotsit");
            car1.setType("Austin");

        trace("modified values of street"); // [trace] modified values of street
            trace("street = " + street); // [trace] street = [Car type = Mini, contents = [Person firstName = Max, secondName = Headroom],[Person firstName = Simon, secondName = Le Bon]],[Car type = Austin, contents = [Person firstName = Harry, secondName = Wotsit]]
            trace("street2 = " + street2); // [trace] street2 = [Car type = Ford, contents = [Person firstName = John, secondName = Doe],[Person firstName = Jane, secondName = Doe]],[Car type = Vauxhall, contents = [Person firstName = Joe, secondName = Bloggs]]

        }

        private function clone(source:*):* {
            var b:ByteArray = new ByteArray();
            b.writeObject(source);
            b.position = 0;
            return(b.readObject());
    }

    }

}

. , - , .

, , .

:

  • .
  • , , , .
  • . , , , . , , , , .

. Adobe : http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_6.html

, Adobe Java.

+6

All Articles