HTML and CSS Reference
In-Depth Information
The bracket notation allows you to use either a string or variable as the index value,
whereas the dot notation requires the literal name of the property.
EXAMPLE 9.7
<script type="text/javascript">
1
cat = new Object();
2
c = "color"
3
cat["name"] = "Powder" ;// same as cat.name = “Powder”
4
cat[c] = "gray"; // same as cat.color = “gray”;
5
document.write( cat.name +"is"+ cat.color + "<br />");
document.write( cat["name"] +"is"+ cat[c] + "<br />");
</script>
EXPLANATION
1
A new cat object is instantiated.
2
The variable c is assigned the string “color”.
3
The square brackets contain an index value that also represents a property for the
object. We could have written this as cat.name = “Powder”.
4
The variable c is used as the index value within the square brackets. Using this
notation would allow you to easily change the value of the variable, thereby
changing the property of the object.
5
A property of the cat object can be accessed directly with the dot notation, or rep-
resented with a string or variable within the square brackets (see Figure 9.9).
Figure 9.9 Properties of the cat object accessed with bracket or dot notation.
9.2.4 Nested Arrays
An array can consist of another set of arrays. Take, for example, a row of seats in a movie
theater. One row of seats would represent a one-dimensional array, but the theater has
more than one row. To get to your seat you need to not only know what row, but the
number of the seat as well.
To create a two-dimensional array, each row is a new array. To find an element in the
array we will use two index values, one for the row and one for the column; for example,
array_name[0][0] represents the first element in the first row. The array in Figure 9.10
consists of three rows and three columns:
 
 
Search WWH ::




Custom Search