Array do you mean a reference or a real Array?

Professeur Pirson Nicolas Pirson :
Go to my website : tatactic.be [FR] (Website about hepatitis)
nicolas.pirson.me [FR - EN] (current website)
business card - blog [FR - EN] (just for fun)
Nicolas Pirson CV page [FR] on tatactic.be
Back to AS3 - FLASH index ... [EN]


Basics : AS1 AS2 and AS3... The Array Object


ActionScript 1 and 2... and 3
Arrays usage : create a new Array or call a reference to another Array?

Level : beginners *

Array... Did tou mean a reference to an Array or a call to a new Array?
an Array is an Array but first it's an Object in the prototype chain!

Array in AS language reference :


See it in the AS1 and AS2 language reference
Object
|
+-Array

http://help.adobe.com/en_US/ content=00000551.html

Array() : Array
Array(numElements:Number) : Array
Array(element0:Object, [element1, element2, ...elementN]) : Array In AS3 :

public dynamic class Array
extends Object

in AS3 :
Package Top Level
Class public dynamic class Array
Inheritance Array Object
(Object is a dynamic class too : public dynamic class Object)

So an Array is an Object too...
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Array.html

First, let' call a refernce to an Array...
A little bit code writing to test this...

MyArray = [];
// or MyArray = new Array();
myNewArray = MyArray;

trace("instanceof Array or is Array in AS3 ? -> " + (myNewArray instanceof Array));
trace("instanceof Object or is Object in AS3 ? -> " + (myNewArray instanceof Object));

/*
This is a reference to MyArray not an instance of a new Array
So if you change something to myNewArray, MyArray will be modified too.
let's take a look at this...
*/


myNewArray[0] = "Some Value";

trace("myNewArray[0] = " + myNewArray[0]);
//Outputs "Some Value"
//this looks fine BUT :

trace("MyArray[0] = " + MyArray[0]);
// Outputs the same as above ("Some Value")

Now, let's make a new Array named myNewArray filled with the values of MyArray.

MyArray = ["Default value"];
// or MyArray = new Array();

myNewArray = MyArray.slice();

// THE FUNCTION slice !
// Returns a NEW array with the values of the Array
// NOT a REFERENCE to the Array!

/*
This is NOT a reference to MyArray but a new Array
So if you change something to myNewArray, MyArray will NOT be modified.
let's take a look at this...
*/

myNewArray[0] = "Some Value";

trace("myNewArray[0] = " + myNewArray[0]);
//Outputs "Some Value"
trace("MyArray[0] = " + MyArray[0]);
// Outputs : "Default value"

MyArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// or MyArray = new Array();

myNewArray = MyArray.slice();
/*
This is NOT a reference to MyArray but a new Array
So if you change something to myNewArray, MyArray will NOT be modified.
let's take a look at this...
*/


trace("myNewArray = " + myNewArray);
//output : 1,2,3,4,5,6,7,8,9
myNewArray = [2, 6, 4, 1, 7, 5, 3, 9, 8, 6];
trace("myNewArray (modified) = " + myNewArray);
// output : 2,6,4,1,7,5,3,9,8,6
trace("MyArray = " + MyArray);
// Outputs 1,2,3,4,5,6,7,8,9 so it's fine, it's not a reference anymore




XHTML 1.0 STRICT. Compatible W3C     CSS 2.0  Compatible W3C