|
Chapter 6
|
Data Field | Content of Field |
name | Advanced JavaScript Programming |
reference | F12345 |
day | Monday |
time | 5 pm - 8 pm |
students | Name, age, address, telephone, other information |
Figure
6-1. The structure of an object—properties and methods.
This data is not static. That is, it can change under certain circumstances. For example, if one of the students decides to quit, you must be able to change the data accordingly. The construction should be able to add new students to the course. These actions are illustrated in Figure 6-1, and listed in the following table:
Table 6-2. Operations in Figure 6-1.
Operation Name | Processed Data |
initialization | Basic information (name, reference, day, etc.) |
addStudent | New student’s basic information |
deleteStudent | Current student’s information |
printCourse | Name and other information regarding the course |
printStudents | Students’ names and other details |
As you can see, the operations process the data stored in the object’s data structures. The object illustrated in Figure 6-1 is named course.
Any physical object has its own unique characteristics. A car, for example, has a size, a weight, a model, a color, a price, and many other attributes. The full set of attributes distinguish a car from all other objects. These features are called properties or fields in the OO (object-oriented) vernacular. An object’s property stores data related to the object. In Figure 6-1, the properties are the “boxes” on the left-hand side of the illustration.
Properties are usually named according to their meaning. For instance, a property that holds the color of the car would be named color. First supported by Netscape Navigator 3.0, it is possible to extend an object by adding new properties to it. Although it is possible to add new properties to an existing object, it is not always possible to modify the value of a static built-in object. For example, the PI property of the Math object cannot be modified, for obvious reasons.
JavaScript supports two different types of objects:
An object can also be a property of another object, thus creating an object hierarchy. We will describe the hierarchical structure later, when we discuss Navigator objects.
Figure
6-2. A sample object hierarchy.
This figure illustrates an object hierarchy, similar to a family tree. a is only an object, while d, e, f, g, h, and i are only properties. b and c are both objects and properties, and therefore, they are located between the other levels of the hierarchy.
An object’s properties hold its data. You can refer to properties using the following syntax:
objectReference.propertyName
objectReference is the name of the object that the property belongs to, or any other valid reference to the object. For example, you can assign an object to a variable and then refer to its properties using this variable, followed by the property specification. propertyName is the name of the property (data field).
A dot separates each object from its property. A hierarchical object structure, with objects and properties at different levels, uses the same syntax:
object1.object2Property1.object3Property2.property3
Notice that all the elements in the middle are both objects and properties. Referring again to Figure 6-2, let’s assume that the elements of the structure have the following values:
d ==> 16 e ==> 42 f ==> true g ==> "king" h ==> 13 i ==> 10
The following statements demonstrate referencing to elements of a hierarchical object structure:
var tempVar = "" tempVar = a.b.d document.write(tempVar) // prints 16 tempVar = a.b.e document.write(tempVar) // prints 42 tempVar = a.c.f document.write(tempVar) // prints true tempVar = a.c.g document.write(tempVar) // prints king tempVar = a.c.h document.write(tempVar) // prints 13 tempVar = a.c.i document.write(tempVar) // prints 10
Previous | Table of Contents | Next |