Arrays, simply put, are objects that can have a series of properties ordered by an index number. Sometimes it matters to have a specific order to these properties, called elements of the array. Sometimes it does not, except to give you a grouping of common elements. (Do not confuse these elements with HTML elements, discussed in Part III, "Document Object Model for HTML Documents.")
Numbered and Unnumbered Properties of Arrays
Suppose you have an object myArray which is an array. Elements of the myArray object, the numbered properties, you can reach by referring to myArray[x], where x is the index number of the array indicating which element you want to reach.
(Only nonnegative integers less than 232 - 1 qualify as index numbers for an array's elements.)
The first element of myArray is myArray. The second is myArray. The nth array element is myArray[n-1]. This is true for all arrays: they begin their element list at 0. The element list for myArray is the collection of all numbered elements, from myArray to myArray[myArray.length-1]. The element list for any array object is defined similarly.
You can always add members to an array's element list. The most common way to do so is to refer to the length property of the array as an element list index number. (The length property, as I will note later in this chapter, is tied to the number of elements in the array; changes to one will reflect in the other.) For example:
myFunc = new Array() // myFunc.length == 0 myFunc[myFunc.length] = new Object() // myFunc is now an object // myFunc.length == 1 myFunc[myFunc.length] = new Object() // myFunc is now an object // myFunc.length == 2
This assigns a value to myFunc[x] for a particular x (in the previous example, x = myFunc.length). At the same time, it extends the length of the array.
Although all elements of the array are properties of the array, not all properties of the array are elements of the array. You can add properties to an array that will not show up in the array's element list. For instance, you can add a method to any array much like you would add a method to other objects:
myArray.myMethod = myFunc
myArray will have the myMethod property, but it will not be between myArray and myArray[myArray.length-1] unless you specifically add it as a member of the element list.
You cannot refer directly to an element's index number:
alert(myArray.0) // throws exception
This can get a bit confusing when you see an error message like this in IE: myArray.0.constructor is not an object. A numbered property like this is always an array's index number. Netscape's error message is clearer: myArray has no properties.
An Array's Elements Can Be Any Object or Value
A new array begins only with the elements explicitly defined in the constructor or literal notation. You can assign to every element of the array whatever object or literal you wish. This includes objects, numbers, strings...whatever you want.
myArray = new Array() myArray = new Array() myArray = new Object() myArray = new Object() // ...
Another use, featured as the example at the end of this chapter, is an extensible version of the switch statement, which is covered in Chapter 11, "The Global Object and Statements." By "extensible," I mean a developer can add cases to the switch at will. A similar concept allows developers to implement an emulation of method overloading, an advanced concept I covered in a limited fashion as the example in Chapter 2, "Function()." Still another use of arrays is my Basket of Windows script, which I feature as the example in Chapter 15, "window."
Populating an Array Automatically
You can define a few elements in the array (populate the array) as you define the array. The first way is to include the elements as arguments in the Array() constructor:
var myArray = new Array("red", "green", "blue")
This makes myArray equal to "red", myArray equal to "blue", and myArray equal to "green". Another way to populate an array is to use the array literal:
var myArray = ["red", "green", "blue"]
The exception to this rule of populating an array is when you use the Array() function and give it a nonnegative integer as its only argument:
var myArray = new Array(3)
This will instead create an array with a length property of 3. On the other hand,
var myArray = new Array("3")
will create an array with a length property of 1, and myArray equal to "3".