
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Named Array Elements
|
255
(push(), pop(), etc., covered later in this chapter) and are not considered part of the
array’s numbered element list. For example, an array with one named element and
two numbered elements will have a
length of 2, not 3. To access all the named ele-
ments in an ActionScript array, therefore, we must use a for-in loop (discussed in
Chapter 8), which lists both named and numbered elements.
An array’s named elements are simply object properties. In Action-
Script, a generic object can hold a group of related properties as legiti-
mately as an array can hold named elements. Both approaches are
common practice.
Creating and Referencing Named Array Elements
To add an element that can later be retrieved by name, we use the familiar square
brackets, with a string in place of the index number, on an existing array:
arrayName[elementName] = expression
where elementName is a string. For example:
var importantDates = new Array(); // Create an empty array
importantDates["dadsBirthday"] = "June 1"; // Add a named element
importantDates["mumsBirthday"] = "January 16"; // Add another named element
Remember to create an empty array using the new Array() constructor before trying
to add named elements to it. We can also use the dot operator to specify a named
array element, as follows: ...