Objects
As described earlier in the chapter, custom objects can easily
be created in JavaScript for use in functions. To do this, you must
first define the object's properties. Once these are defined, new
instances of the object can be created and new
Defining an Object
To define a new object, you must first outline its properties.
Let's define an object named
contact
and set a few of its
properties, as in
function contact(name, telephone, title) {
this.name = name;
this.telephone = telephone;
this.title = title;
}
This results in a new contact object that is defined along with its properties. The contact object can now be created using the new statement.
contact1 = new contact("Mabel Chin","(781) 333-5252","Estate Attorney");
This creates an object called contact1 with three bound properties: contact1.name , contact1.telephone , and contact1.title . This method is called the instance of the object. Let's create another contact using the new statement once again:
contact2 = new contact("Silke Hase","(781)384-5470)","Development Manager");
When creating contact2 , which is actually just another new instance of the contact object, contact2 's properties are very independent of those in contact1 . Here's another way to set the properties of an object: Contact2.birthday = "April 5"
This adds the
birthday
property to
contact2
without
function contact(name, telephone, title,
birthday
) {
this.name = name;
this.telephone = telephone;
this.title = title;
this.birthday = birthday;
}
Objects as Properties of ObjectsIt is also possible to create objects as properties to other objects. This means associating an object's property with another object. For example, let's create a company object:
function company(name, city, state){
this.name = name;
this.city = city;
this.state = state;
}
Now let's create two new instances of the company object:
company1 = new company("Fancy Bank","Bedford","MA");
company2 = new company("Emerging Innovations","Stoneham","MA");
Using these two new company objects, we can easily create the following new contact objects adding the company 's properties to each:
contact1 = new contact("Mabel Chin","(781) 333-5252","Estate Attorney","September 17",
By making the company properties part of the contact object, it is now easy to refer to the company properties for each contact object: contact2.coname Adding Methods to ObjectsMethods can be added to an object's definition by creating functions that define the method. Methods are merely functions associated with an object. Let's add a method called displaycontact to the contact object that will print the contact's name, phone, title, birthday, company, city, and state to a document window:
Function displaycontact(contact) {
document.write("Contact Name:\t" + this.name + "<BR>");
document.write("Telephone:\t" + this.telephone + "<BR>");
document.write("Title:\t\t" + this.title + "<BR>");
document.write("Birthday:\t" + this.birthday + "<BR>");
document.write("Company:\t" + this.coname + "BR>");
document.write("City:\t\t" + this.city + "<BR>");
document.write("State:\t\t" + this.state + "<BR>");
}
To use this method, it must be included in the contact object's definition:
function contact(name,telephone,title,birthday,coname,city,state){
this.name = name;
this.telephone = telephone;
this.title = title;
this.birthday = birthday;
this.coname = coname;
this.city = city;
this.state = state;
this.displaycontact = displaycontact;
}
The following command would be used to output contact2 's information to the document window: contact2.displaycontact(); Figure 16.17 shows what the output looks like in a document window. Figure 16.17. The contact2 object's properties output to the browser.
|