Java Reference
In-Depth Information
two times, you would need to check whether the minutes of the arrival time were greater than the min-
utes of the departure. If so, you can simply subtract the departure time minutes from the arrival time
minutes. If not, you need to add 60 to the arrival time minutes and subtract one from the arrival time
hours to compensate, before taking the departure time minutes from the arrival time minutes. You then
need to subtract the departure time hours from the arrival time hours, before putting the minutes and
hours that you have arrived at back together.
This would work okay so long as the two times were in the same day. It wouldn't work, for example,
with the times 23:45 and 04:32.
This way of working out the time difference obviously has its problems, but it also seems very complex.
Is there an easier way to deal with more complex data such as times and dates?
This is where objects come in. You can defi ne your departure and arrival times as Date objects. Because
they are Date objects, they come with a variety of properties and methods that you can use when you need
to manipulate or calculate times. For example, you can use the getTime() method to get the number of
milliseconds between the time in the Date object and January 1, 1970, 00:00:00. Once you have these
millisecond values for the arrival and departure times, you can simply subtract one from the other and
store the result in another Date object. To retrieve the hours and minutes of this time, you simply use the
getHours() and getMinutes() methods of the Date object. You'll see more examples of this later in
the chapter.
The Date object is not the only type of object that JavaScript has to offer. Another object type was
introduced in Chapter 2, but to keep things simple, we didn't tell you what it was at the time: the Array
object. Recall that an array is a way of holding a number of pieces of data at the same time.
Array objects have a property called length that tells you how many pieces of data, or rather how many
elements, the array holds. You actually used this property in the trivia quiz in Chapter 3 to work out how
many times you needed to loop through the array.
Array objects also have a number of methods. One example is the sort() method, which can be used
to sort the elements within the array into alphabetical order.
You should now have an idea why objects are useful in JavaScript. You have seen the Date and Array
objects, but there are many other types of objects that JavaScript makes available so that you can achieve
more with your code. These include the Math and String objects, which we will talk more about later in
the chapter.
Using JavaScript Objects
Now that you have seen the why of JavaScript objects, you need to look at the what and the how .
Each of JavaScript's objects has a collection of related properties and methods that can be used to manipu-
late a certain kind of data. For example, the Array object consists of methods to manipulate arrays and
properties to fi nd out information from them. In most cases, to make use of these methods and properties,
you need to defi ne your data as one of these objects. In other words, you need to create an object.
In this section, you'll look at how to go about creating an object and, having done that, how you use its
properties and methods.
Search WWH ::




Custom Search