Objects

The object model provides us with an interface to the browser that masks us from what actually goes on beneath the hood of the browser, such as how browser operations are accomplished. This interface is embodied in the set of objects that make up the object model. But what actually is an object in computer terms?

While an object can be anything from an apple to a television in the real world, to keep things simple, in computer terms, we shall just consider an object to be a combination of data and code. Objects are self-contained parts of a program, which carry out specific functions. The data and code associated to the object define everything that the object represents (state) and what it can do (behavior). The characteristics or blueprint of an object is specified by a class definition. An object, of which there may be many, is a created instance of a class, just as an instance of a cake can be created from a recipe. IE4 is made up from many interrelated objects and indeed even the whole browser itself is considered an object.

An Object's Interface

All object models, including the browser object model, are made up of a set of interconnected and interrelated objects. Fortunately for us, while these objects might represent anything, they all operate in the same way to a script developer. It's this similarity that makes objects so powerful. Our script code can access every object with similar syntax, without regard for what the object represents. Furthermore, code can only interact with objects in predefined ways – through properties, methods, and events. A standard way of communicating is a key concept in any set of objects.

Properties

A property of an object is an attribute that somehow reflects the current state of the object. As an example, suppose we were working with a Car object. This object might have properties with names like NumberOfDoors, BodyStyle, CurrentSpeed, and LightsOn. Some propertiescan only be read – they can't be changed. Others can be read and written. Many of the Car object properties are read-only; that is, they were set when the car was built and can't be changed (at least without a lot of work!). Other properties, like CurrentSpeed and LightsOn, can be changed or retrieved; these properties are read/write properties.

While we've said many times that we won't provide any more than the most rudimentary code in these chapters, it's important to understand on the simplest scale how properties are called. Assuming we had an instance of our object called MyCarObject, we could display the read-only properties we invented in the previous paragraph in VBScript like this:

MsgBox MyCarObject.NumberOfDoors
MsgBox MyCarObject.BodyStyle

Setting read/write properties is equally as simple:

MyCarObject.CurrentSpeed = 55
MyCarObject.LightsOn = True

We can read these variables in the same manner that we access read-only variables:

MsgBox MyCarObject.CurrentSpeed
MsgBox MyCarObject.LightsOn

Methods

Properties are great if we're only interested in finding out what state an object is in or in changing a discrete characteristic of an object.. Fortunately we have methods to help us out of this situation. In short, objects provide methods so that they can be instructed to carry out certain predefined operations. Our Car object could have methods like SpeedUp, SlowDown, TurnRight, TurnLeft, and Stop. These methods could take any number of parameters (perhaps the miles per hour to change speed or the degrees to turn). We use methods to influence the current state of the object.

Calling methods with VBScript is simple:

MyCarObject.SpeedUp 20  'speed up 20mph
MyCarObject.TurnLeft 90 'turn left 90 degrees

Events

Both properties and methods are things that we, as an outside force, do to an object. Events, in contrast, are things that an object does to its container or host. More specifically, they're entities that allow us to respond to some condition or state inside an object. A Car object might have, among other things, a FuelLow event that would be raised when the fuel level in the car fell below some level, a FuelEmpty event raised when the fuel tank was empty, and a Collision event – which we hope we never see!

There are a few different ways to connect code to events with VBScript. In our simple examples we'll use the same style that Visual Basic uses:

Sub MyCarObject_FuelLow
   ...      'switch the fuel-low light on
End Sub

Sub MyCarObject_Collision
   ...      'call the insurance company
End Sub

Collections

The final object concept we need to understand before digging into the object model is the idea of a collection – an easy way to group related objects together so they can be on as a unit. A car rental agency might keep a few collections of Car objects around. These collections might be called names like AvailableCars, RentedCars, and WreckedCars (these cars would probably have seen at least one Collision event). Collections are objects in their own right, and they have their own properties to show the status of the collection (like Length – the number of elements in the collection). They can also have their own methods and events, which will operate on the collection as a whole.