1109Chapter 41 .Functions and (Post office web site) Custom Objects You actually
1109Chapter 41 .Functions and Custom Objects You actually had a preview of this power in Chapter 37 s discussion about arrays. An array, you recall, is an ordered collection of data. You can create a JavaScript array in which entries are labeled just like properties that you access via the now- familiar dot syntax (arrayName[index].propertyName). An object typically contains different kinds of data. It doesn t have to be an ordered collection of data although your scripts can use objects in constructions that strongly resemble arrays. Moreover, you can attach any number of custom functions as methods for that object. You are in total control of the object s structure, data, and behavior. An example planetary objects Building on your familiarity with the planetary data array created in Chapter 37, this chapter shows you how convenient it is to use the data when it is constructed in the form of custom objects. The application goal for the extended example in this section is to present a pop-up list of the nine planets of the solar system and display data about the selected planet. From a user-interface perspective (and for more exposure to multiframe environments), the resulting data displays in a separate frame of a two-frame window. This means your object method builds HTML on the fly and plugs it into the display frame. If you implement this application strictly for IE4+ and NN6, you can apply the same data to reconstruct the displayed table data for each user selection. The example as shown, however, is fully backward- compatible for all scriptable browsers. In this chapter, instead of building arrays to hold the data, you build objects one object for each planet. The design of your object has five properties and one method. The properties of each planet are: name, diameter, distance from the sun, year length, and day length. To assign more intelligence to these objects, you give each of them the capability to display their data in the lower frame of the window. You can conveniently define one function that knows how to behave with any of these planet objects, rather than having to define nine separate functions. Listing 41-5 shows the source code for the document that creates the frameset for your planetary explorations; Listing 41-6 shows the entire HTML page for the object-oriented planet document, which appears in the top frame. Listing 41-5: Framesetting Document for a Two-Frame Window
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.