Java Reference
In-Depth Information
</body>
</html>
If you load it into your web browser, you'll see three lines written into the page, which represent the
name, age, and address of the person whose details are stored in the personnel[1] element of the
array, as shown in Figure 2-12.
Name : Name1
Age : Age1
Address : Address1
Figure 2-12
The fi rst thing to do in this script block is declare a variable, personnel, and tell JavaScript that you
want it to be a new array.
var personnel = new Array();
Then you do something new; you tell JavaScript you want index 0 of the personnel array, that is, the ele-
ment personnel[0] , to be another new array.
personnel[0] = new Array();
So what's going on? Well, the truth is that JavaScript doesn't actually support multi-dimensional arrays,
only single ones. However, JavaScript enables us to fake multi-dimensional arrays by creating an array
inside another array. So what the preceding line is doing is creating a new array inside the element with
index 0 of our personnel array.
In the next three lines, you put values into the newly created personnel[0] array. JavaScript makes
it easy to do this: You just state the name of the array, personnel[0] , followed by another index in
square brackets. The fi rst index ( 0 ) belongs to the personnel array; the second index belongs to the
personnel[0] array.
personnel[0][0] = “Name0”;
personnel[0][1] = “Age0”;
personnel[0][2] = “Address0”;
After these lines of code, your array looks like this:
Index
0
0
Name0
1
Age0
2
Address0
The numbers at the top, at the moment just 0, refer to the personnel array. The numbers going down
the side, 0, 1, and 2, are actually indices for the new personnel[0] array inside the personnel array.
Search WWH ::




Custom Search