Arrays in JScript are sparse. That is, if you have an array with three elements that are numbered 0, 1, and 2, you can create element 50 without worrying about elements 3 through 49. If the array has an automatic length variable (see Intrinsic Objects for an explanation of automatic monitoring of array length), the length variable is set to 51, rather than to 4. You can certainly create arrays in which there are no gaps in the numbering of elements, but you are not required to.

In JScript, objects and arrays are almost identical to each other. The two main differences are that normal objects do not have an automatic length property, and arrays do not have the properties and methods of an object.

Addressing Arrays

You address arrays by using brackets "[]". The brackets enclose either a numeric value, or an expression that evaluates to a whole number. The following example assumes that the entryNum variable is defined and assigned a value elsewhere in the script.

 CopyCode imageCopy Code
theListing = addressBook[entryNum];
theFirstLine = theListing[1];

Objects as Associative Arrays

Normally, you use the dot operator "." to access an object's properties. For example,

 CopyCode imageCopy Code
myObject.aProperty

Here, the property name is an identifier. You can also access an object's properties using the index operator "[]". Here, you are treating the object as an associative array. An associative array is a data structure that allows you to dynamically associate arbitrary data values with arbitrary strings. For example,

 CopyCode imageCopy Code
myObject["aProperty"] // Same as above.

Although the use of the index operator is more commonly associated with accessing array elements, when used with objects, the index is always the property name expressed as a string literal.

Notice the important difference between the two ways of accessing object properties.

Operator The property name is treated as Meaning the property name

Dot "."

an identifier

cannot be manipulated as data

Index "[]"

a string literal

can be manipulated as data

This difference becomes useful when you do not know what the property names will be until runtime (for example, when you are constructing objects based on user input). To extract all the properties from an associative array, you must use the for in loop.

In Vbsedit, you only need to press F1 to get Help for the keyword under the cursor!


Download Now!



Download   Home  

Copyright © 2001-2024 adersοft