Java Reference
In-Depth Information
What Are Objects?
To start the introduction to objects, let's think about what is meant by an object in the “real world” out-
side computing. The world is composed of things, or objects, such as tables, chairs, and cars (to name
just a few!). Let's take a car as an example, to explore what an object really is.
How would you defi ne the car? You might say it's a blue car with four-wheel drive. You might specify the
speed at which it's traveling. When you do this, you are specifying properties of the object. For example,
the car has a color property, which in this instance has the value blue.
How do you use the car? You turn the ignition key, press the gas pedal, beep the horn, change the gear
(that is, choose between 1, 2, 3, 4, and reverse on a manual car, or drive and reverse on an automatic),
and so on. When you do this, you are using methods of the object.
You can think of methods as being a bit like functions. Sometimes, you may need to use some informa-
tion with the method, or pass it a parameter, to get it to work. For example, when you use the changing-
gears method, you need to say which gear you want to change to. Other methods may pass information
back to the owner. For example, the dipstick method will tell the owner how much oil is left in the car.
Sometimes using one or more of the methods may change one or more of the object's properties. For
example, using the accelerator method will probably change the car's speed property. Other properties
can't be changed: for example, the body-shape property of the car (unless you hit a brick wall with the
speed property at 100 miles per hour!).
You could say that the car is defi ned by its collection of methods and properties. In object-based pro-
gramming, the idea is to model real-world situations by objects, which are defi ned by their methods
and properties.
Objects in JavaScript
You should now have a basic idea of what an object is — a “thing” with methods and properties. But
how do you use this concept in JavaScript?
In the previous chapters you have (for the most part) been dealing with primitive data (that is, you've
been working with actual data). This type of data is not too complex and is fairly easy to deal with.
However, not all information is as simple as primitive data. Let's look at an example to clarify things
a little.
Suppose you had written a web application that displayed timetable information for buses or trains.
Once the user has selected a journey, you might want to let him know how long that journey will take.
To do that, you need to subtract the arrival time from the departure time.
However, that's not quite as simple as it may appear at fi rst glance. For example, consider a departure
time of 14:53 (for 2:53 p.m.) and an arrival time of 15:10 (for 3:10 p.m.). If you tell JavaScript to evaluate
the expression 15.10-14.53, you get the result 0.57, which is 57 minutes. However, you know that the real
difference in time is 17 minutes. Using the normal mathematical operators on times doesn't work!
What would you need to do to calculate the difference between these two times? You would fi rst need
to separate the hours from the minutes in each time. Then, to get the difference in minutes between the
Search WWH ::




Custom Search