
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
256
|
Chapter 11: Arrays
As a matter of good form, you should always avoid using illegal identifiers as named
elements, despite the flexibility of the
[] operator.
Removing Named Elements
To rid an array of an unwanted named element, we use the delete operator, which we
introduced in Chapter 5:
delete arrayName.elementName
Deleting a named element destroys both the element value and the element con-
tainer, freeing up any memory being occupied by the element and its contents. (Con-
trast this with the delete operator’s behavior for numbered elements, where it simply
clears the value but leaves the container intact.)
Adding Elements to an Array
You can add elements to an array by specifying a value for a new element, increasing
the array’s
length property, or using one of the built-in array functions.
Adding New Elements Directly
We can add a new element to an existing array at a specific index simply by assign-
ing a value to that element:
// Create an array, and assign it three values
var myList = ["apples", "oranges", "pears"];
// Add a fourth value
myList[3] = "tangerines";
The new element does not need to be placed immediately after the last element of the
old array. If we place the new element more than one element beyond the end of the
array, ActionScript automatically creates undefined