Space web hosting - 164Part IIIDocument Objects ReferenceHierarchy as road mapFor the
164Part IIIDocument Objects ReferenceHierarchy as road mapFor the programmer, the primary role of the document object hierarchy is to provide scriptswith a way to reference a particular object among all the objects that a browser window cancontain. The hierarchy acts as a road map the script can use to know precisely which objectto address. Consider, for a moment, a scene in which you and your friend Tony are in a high school class- room. It s getting hot and stuffy as the afternoon sun pours in through the wall of windows onthe west side of the room. You say to Tony, Would you please open a window? and motionyour head toward a particular window in the room. In programming terms, you ve issued acommand to an object (whether or not Tony appreciates the comparison). This human inter- action has many advantages over anything you can do in programming. First, by making eyecontact with Tony before you speak, he knows that he is the intended recipient of the com- mand. Second, your body language passes along some parameters with that command, point- ing ever so subtly to a particular window on a particular wall. If, instead, you are in the principal s office using the public address system, and you broad- cast the same command, Would you please open a window?, no one knows what you mean. Issuing a command without directing it to an object is a waste of time because every objectthinks, That can t be meant for me. To accomplish the same goal as your one-on-one com- mand, the broadcast command has to be something like, Would Tony Jeffries in Room 312please open the middle window on the west wall? Let s convert this last command to JavaScript dot syntaxform (see Chapter 4). Recall from thetutorial that a reference to an object starts with the most global point of view and narrows tothe most specific point of view. From the point of view of the principal s office, the locationhierarchy of the target object isroom312.Jeffries.TonyYou can also say that Tony s knowledge about how to open a window is one of Tony s meth- ods. The complete reference to Tony and his method then becomesroom312.Jeffries.Tony.openWindow() Your job isn t complete yet. The method requires a parameter detailing which window toopen. In this case, the window you want is the middle window of the west wall of Room 312. Or, from the hierarchical point of view of the principal s office, it becomesroom312.westWall.middleWindowThis object road map is the parameter for Tony s openWindow()method. Therefore, theentire command that comes over the PA system isroom312.Jeffries.Tony.openWindow(room312.westWall.middleWindow) If, instead of barking out orders while sitting in the principal s office, you attempt the sametask via radio from an orbiting space shuttle to all the inhabitants on Earth, imagine howlaborious your object hierarchy is. The complete reference to Tony s openWindow()methodand the window that you want opened has to be mighty long to distinguish the desiredobjects from the billions of objects within the space shuttle s view. The point is that the smaller the scope of the object-oriented world you re programming, themore you can assume about the location of objects. For client-side JavaScript, the scope is nowider than the browser itself. In other words, every object that a JavaScript script can work