Using objects

So far in this article, we have seen an abstract example of how to create objects, but little discussion has been devoted to why objects would be desirable to use. Quite simply, objects organise information in a manner that is easy to use. In situations where you have large amounts of similar information that need to be made available to users, objects can help manage that data.

Let's look at an example that is more applicable to a real-life situation. Over the past two years, there has been a great deal of change and expansion at APC. With this growth, there has also been a restructuring of responsibilities, and it is sometimes difficult for readers to know who the appropriate person is to contact here. Further, there is no easy way of tracking down the information (OK, so it's printed in the magazine, but who reads that page anyway?). One possible solution might be to create a Web page with each staff member's contact details on it.

For the contact page to be usable, it would have to be current, which means that it would have to be easy to maintain. Further, it should be flexible, so that information could be easily added and deleted as necessary. Finally, we would want the page to be useful.

To begin working on this project, I separated the necessary data into separate groups (objects). The information is going to be presented on a form, which the JavaScript language has already defined. The second most obvious object is an employee object, which contains information on each individual employee. Finally, all the employees have to be grouped together so they can be worked on as a whole.

The next step is to plan the employee object, and decide what information it needs to contain. This example is fairly straightforward - we need to track the employee's name, area of primary responsibility, phone number and email address. It would also be nice if each employee object had a showPerson() method that displayed the appropriate information on the form. The employee object could be defined by the following code:

function empl(employeeName)
{
this.name = employeeName;
this.area = "";
this.phone = "";
this.email = "";
this.showPerson = showPerson;
}

The method's name is empl(), and not employee(), as would be expected. I chose empl because all of the empl objects are going to be collected in an array named employee. Since most of the code will be operating on the array of objects, and not the objects themselves, it makes more sense to be able to use the following syntax:

alert("Employee's email address: " + employee[0].email);

Finally, each empl object has to be instantiated. For example, Lachlan's object can be defined like this:

var lbott = new empl("Botticchio, Lachlan");
lbott.area = "Technical Editor";
lbott.phone = "(02) 9288 9199";
lbott.email = "lb@acp.com.au";

The benefit of this approach is that it is easy to maintain. The objects make it easy to adjust to restructuring, and new information can be added easily. The full implementation can be found in the examples following this section.