Once you've read Chapter 12, you should recognize that arrays are a type of object, specifically, instances of the Array class. As with other objects, Array objects can have properties added to them. When we add named elements to an array, we're really just adding properties to an Array object. However, as we saw earlier, to use the built-in methods of the Array class we must work with numbered elements.
In Chapter 9, we saw a useful "array-object" hybrid, the arguments object, that combined numbered elements with properties. (Recall that the arguments object has callee and caller properties but also stores an array of parameters as array elements.) Now we can see that all arrays contain their numbered list of elements, plus the built-in property length, plus any custom properties we add.
In addition, because arrays are instances of the Array class, we can add new Array methods that work on all arrays. For example, the following code creates a method named searchArray( ), based on the function we created earlier in Example 11-2.
// Add a new searchArray( ) method to all arrays Array.prototype.searchArray = function (searchElement) { // Check each element to see if it contains searchElement for (var i = 0; i < this.length; i++) { if (this[i] = = searchElement) { return i; } } return null; }; // Create an array var soundtracks = ["electronic", "hip hop", "pop", "alternative", "classical"]; // Use the searchArray method to find the index of the word "classical" trace(soundtracks.searchArray("classical")); // Displays: 4
To determine whether an object is an array, use the instanceof operator. For example:
var colors = ["red", "blue", "green"]; trace(colors instanceof Array); // Displays: true