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.
NO
TE
There is no advantage to using the Array constructor to create an associative array. The
Array constructor is best for creating indexed arrays.
NO
T
E
There is no advantage to using the Array constructor to create an associative array.
The Array constructor is best for creating indexed arrays.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...