
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
250
|
Chapter 11: Arrays
The Array Constructor
To create an array with the Array() constructor, we use the new operator, followed
by the word
Array, followed by parentheses, which yields an empty array (one with
no elements). As we’ll see in Chapter 12, constructor functions create a new data
object, in this case an Array instance. We normally assign a newly created array to a
variable or other data container for future reference. For example:
var myList = new Array(); // Store an empty array in variable myList
We often want to assign initial values to an array’s elements. We can do so by pass-
ing parameters to the Array() constructor when invoking it. Depending on the
parameters we supply, the constructor invocation has different effects.
When we supply more than one argument to the Array() constructor, or when we
supply a single nonnumeric argument to the Array() constructor, each argument
becomes one of the element values in our new array. For example:
var frameLabels = new Array("intro", "section1", "section2", "home");
The array stored in frameLabels has the following elements:
0: "intro"
1: "section1"
2: "section2"
3: "home"
When we supply exactly one numeric argument to the Array() constructor, it creates
an array with the specified number of empty placeholder elements:
var myList ...