JavaScript: The Definitive Guide

Previous Chapter 8
Arrays
Next
 

8.3 Array/Object Dual Nature

Throughout this book, we've been treating objects and arrays as if they were separate data types. This is a useful and reasonable simplification, and you can treat them as separate data types for most of your JavaScript programming. To fully understand the behavior of objects and arrays, however, you have to know the truth: objects and arrays are the same thing.

You can verify this with the typeof operator--use it with any array or with any object, and it returns the string "object". Because arrays and objects are the same thing, any object can have numerically indexed array elements, and any array can have named properties:

o.prop = "property1"
o[1] = "element1"
a[3] = a[2] + a[1];
a.size = 3;

Note, however, that because of the ways arrays and objects are implemented in Navigator 2.0, there are some nonobvious consequences of mixing properties and elements that you must beware of. These, and other features of arrays in Navigator 2.0, are discussed later in this chapter.


Previous Home Next
Multidimensional Arrays Book Index Creating Arrays

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell