Java Reference
In-Depth Information
objeCt‐based programming
Object‐based programming is a slightly scarier way of saying “programming using objects.” But
what are these objects that you will be programming with? Where are they and how and why would
you want to program with them? In this section, you look at the answers to these questions, both in
general programming terms and more specifically within JavaScript.
What are objects?
To start the introduction to objects, let's think about what is meant by an object in the “real world”
outside 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 define 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
information 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 defined by its collection of methods and properties. In object‐based
programming, the idea is to model real‐world situations by objects, which are defined 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.
 
Search WWH ::




Custom Search