Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

Chapter 6
Object-Based Programming

Objects

In a nutshell, an object is a programming abstraction that groups data with the code that operates on it. All programs contain data of different types. Variables, as well as functions, were introduced separately in the previous chapter. We defined functions, and we defined variables, but we did not join them in any way. An object encapsulates related data and functions into a single cohesive unit.

Objects are relatively new to the computer programming industry. Traditionally, code and data have been kept apart. For example, in the C language, units of code are called functions, while units of data are known as structures. Functions and structures are not formally connected in C. A C function can operate on numerous data structures, and more than one function can operate on the same structure. In object-oriented (OO) and object-based programming, code and data are merged into an object, which is a single indivisible “creature.”

Figure 6-1 on the following page shows several data structures and related operations—the exact definition of an object. All data is located on the left-hand side of the illustration. Operations are located on the right-hand side. The data is stored as follows:

Table 6-1. Data structures in Figure 6-1.


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.

Properties

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:

  • Predefined built-in objects, such as the Math object
  • User-defined objects, such as the course object illustrated in Figure 6-1.

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.

Syntax

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


With any suggestions or questions please feel free to contact us