What is the difference between - Object and Array? When should I use Array and when to use the Object?
Please give examples - how to use them correct.
When we talk about an Object in ActionScript 3, it is important to note that everything inherits Object and that a vanilla Object instance is dynamic (meaning you can freely attach data to it without that data being defined within the class definition).
You are able to initialize a vanilla object using new Object() or the { } object literal syntax.
A dynamic object is a key:value store, meaning it stores values against strings that can be used to later reference a value. Values can be set or retrieved this way using dot notation (object.key) or square bracket notation (object['key']). The main difference between the two is that with square bracket notation, you are able to:
A full example of all of this would be something like:
var data:Object = { };
data.firstName = 'John';
data['lastName'] = 'Smith';
trace(data['firstName'] + ' ' + data.lastName); // John Smith
The object literal syntax is able to accept key:value pairs as well, meaning the above could be simplified to:
var data:Object = { firstName: 'John', lastName: 'Smith' };
Iterating over the key:value store of an Object is achieved using the for..in loop:
for(var prop:String in data)
{
trace(prop, data[prop]);
}
Note that based on the above, we know that Array directly inherits Object. The Array class is also dynamic, which means you are able to use the same key:value storage as above with Object. This is where it starts getting confusing.
Arrays can be initialized using:
new Array()[] array literal syntax.Array() function.Arrays are able to store sequential data vs an arbitrary collection of values. Although the values we added to the data object above appear sequential, they are actually stored in no particular order.
Arrays store sequential data using a zero-based numeric index in place of 'keys', something like this:
var array:Array = [];
array[0] = 'hello';
trace(array[0]); // hello
Arrays provide methods for dealing with adding and removing elements from the head and tail of the array, like push(), pop(), shift() and unshift(). Arrays are also able to offer their length, representing the number of elements in that array.
The confusion arises when you start using non-numeric keys with an array:
array['prop'] = 10;
Instead of adding an element to sequential list, we actually go back to the Object that the array inherits from an add an arbitrary prop key with the value 10. In this case, we have one item in the array (the string hello), and a dynamic property named prop attached to the array instance itself (not in the array).
To iterate over an array of items, we can use either a normal for loop or the for each loop:
for(var index:int = 0; i < array.length; i++)
{
trace(array[index]);
}
// Or:
for each(var value:String in array)
{
trace(value);
}
It is important to note that the for each loop only works if all of your values in the array are of the same type used in the definition for that loop (String in this case).
Vectors are a specialized sequential data store that allows you to benefit from static type checking by defining what type of objects you will be storing upfront. For example, we can create a list of Sprite objects using the following syntax:
var sprites:Vector.<Sprite> = new <Sprite>[];
Because Vectors are typed, adding a non-Sprite value to the list will give you a compile-time error, saving a lot of potential bugs. Vectors also provide faster read and write access than Arrays.
Array is used for numerical access, and is zero-based (0 is first element)
var array:Array = new Array("a", "b", "c");
array[0] = "z"; // change first element to z
array.push("d"); // add d to end
trace(array[3]) // output: d
trace(array) // output: z,b,c,d
Arrays can be looped through.
for (var i:int = 0; i < array.length; i++)
{
trace(array[i]);
}
Object can be used for a variety of things. Compared with Array, it can allow for associative arrays (using strings for keys instead of integers)
var assocArray:Object = {me:"Mike", you:"Gabriel"};
// property-style access
trace( assocArray.me ) // output Mike
// access with string
trace( assocArray["you"] ) // Gabriel