As noted above, the implementation of arrays in Navigator 2.0 is substantially different than that in either Navigator 3.0 or Internet Explorer 3.0. One of the differences we've seen is that there is no Array() constructor in Navigator 2.0, and so you may want to write your own constructor function. Similarly, you may want to manage the value of a length (or size) property yourself.
But the biggest difference between Navigator 2.0 and 3.0 is in how array elements and object properties interact. In both versions of Navigator, arrays and objects are the same basic data type: objects can have array elements, and arrays can have object properties. The difference is that in Navigator 2.0, elements and properties can overwrite each other; in Navigator 3.0 they can't.
In Navigator 2.0, a newly defined property takes up the "slot" of the next available array element. Thus after executing the following lines, in the Navigator 2.0 browser, person.name is the same as person[0], and person.address is the same as person[1]:
person = new Object(); person.name = "david"; person.address = "somewhere on the internet";
If there are already some array elements defined in the object, then a new property takes up the element after the highest element already defined (even if there are undefined elements with lower indexes). So in the following code, in Navigator 2.0, address.zip is the same as address[4]:
address = new Object(); address[3] = "Anytown, USA"; address.zip = 22222;
The implication of all this is that if you define properties and later set array elements (for example, address[4] = 66666), you may inadvertently be overwriting the value of your properties. This can lead to strange bugs that are difficult to find.
Note that if, for any given object, you use only object properties, or only array elements, then you won't encounter this overlap problem. But, as we've seen, it is common to use a length property (or a size property) in conjunction with arrays. We must be careful to do this correctly. The convention for most Navigator 2.0 JavaScript code is to use array element 0 to hold the length property, and then to begin the array contents themselves with element 1. Thus, in Navigator 2.0 it is common to see array constructors like the one we saw above:
function EmptyArray(length) { this.size = length; for(var i = 1; i <= length; i++) this[i] = 0; }
The crucial feature of this constructor is that it assigns a value to the size[3] property before it initializes any of the array elements. Creating this size property uses up element 0 of the array, so the loop initializes the array starting with element 1. If, instead, we had initialized the array and then set the size property, then that property would have been at the end of the array. If we later added more elements to the array, we would overwrite the value of size. Of course, if we know that our array has a fixed size and will never be made larger, then there would be no problem with doing it this way, and it allows us to begin the array with element 0 instead of element 1.
[3] We use a size property here instead of length to avoid confusion with Navigator 3.0 arrays that have an automatically updated length property. By using a different name we won't expect the property to be automatically updated.
As we've seen, another difference between arrays in Navigator 2.0 and 3.0 is that in Navigator 3.0, arrays created with the Array() constructor have their length property automatically updated when new elements are added to the array. If you need this feature in Navigator 2.0, you'll have to implement it yourself. You can do it with code like this:
a[i] = j; if (i > a.size) a.size = i;
Despite all this discussion of the array length property, and the ways to simulate it in Navigator 2.0, don't forget that there are many algorithms and uses for arrays in which a size or length property is not necessary. When this is possible, you can simply not bother with a size property. If your array has no object properties assigned, you don't have to worry about about overwriting array elements. And when your algorithm does require you to keep track of the size of your array, an obvious alternative to a size property is to maintain the array length in a separate variable, independent of the array. This also avoids the problem of properties overwriting elements.
Finally, one further feature of arrays in Navigator 2.0 is that they can be indexed using object notation. Just as object properties can be accessed with the . operator and a literal property name or the [] operator and a property name expressed as a string, so too can Navigator 2.0 arrays be accessed with either operator. When using the traditional array [] operator, the index can be any expression that evaluates to a positive integer. When using the . operator, the index must be an integer literal. So, in Navigator 2.0, the expression a.2 is legal and is equivalent to a[2]. Using the . operator is not at all recommended--this bizarre feature of the language is deprecated, and has been removed in Navigator 3.0.
This HTML Help has been published using the chm2web software. |