About arrays 175
The second way to create an associative array is to use the Array constructor and then use
either the bracket operator (
[]) or the dot operator (.) to add key and value pairs to the array.
If you declare your associative array to be of type Array, you cannot use an object literal to
initialize the array.
The next example demonstrates how to use the Array constructor to create an
associative array.
To create an associative array using the Array constructor:
1. Create a new Flash document, and save it as assocArray2.fla.
2. Add the following ActionScript to Frame 1 of the Timeline:
var monitorInfo:Array = new Array();
monitorInfo["type"] = "Flat Panel";
monitorInfo["resolution"] = "1600 x 1200";
trace(monitorInfo["type"] + ", " + monitorInfo["resolution"]);
This code creates an associative array named monitorInfo using the Array constructor
and adds a key called
type and a key called resolution, along with their values.
3. Select Control > Test Movie.
The Output panel displays the following text:
Flat Panel, 1600 x 1200
Associative arrays are essentially instances of the Object class, and there is no advantage of
creating associative arrays using the Array constructor. Even though you create an associative
array using the
new Array() constructor, you cannot use any of the Array class’s methods and
properties (such as
sort() or length) when using an associative array. If you want to use key/
value pairs instead of a numeric index, you should use the Object class instead of an
associative array.
NOTE
There is no advantage to using the Array constructor to create an associative array. The
Array constructor is best for creating indexed arrays.
NOTE
There is no advantage to using the Array constructor to create an associative array.
The Array constructor is best for creating indexed arrays.