Building an Address Book > The Contact Class |
![]() ![]() ![]() |
The Contact Class
The Contact class is a container for properties. By using this container, we can neatly store information about people in the address book. Each Contact object has the following properties:
firstName The person's first name. lastName The person's last name. email The person's email address. phone The person's phone number. phonetype The type of phone number (home: 0, work: 1, cell: 2). company The person's place of business. comments Arbitrary notes associated with the person. id A unique id by with to identify the contact record. dateAdded Creation time of the contact record.
For simplicity, the Contact
class does not provide any methods
to access the properties of its objects. Properties are directly retrieved and
set. The entire class consists of a series of property assignments in the Contact
constructor. The constructor's arguments supply most of the property values
associated with each Contact
object. Arguments are checked for
validity as follows:
// If the argument is a string, assign it to the firstName // property. Otherwise, assign the empty string as the value // of the firstName property. this.firstName = (typeof firstName == "string") ? firstName : "";
The constructor also assigns a dateAdded
property that stores a Date
object representing the current time.
The Contact
constructor is called only by the ContactManager
's addContact()
method.
The complete listing for Contact
class is shown below.
/* ================= * Class constructor * ================= * Desc: The Contact constructor is not called directly by the * user, but by the addContact() method of the ContactManager class. * Parameters: * firstName The person's first name. * lastName The person's last name. * email The person's email address. * phone The person's phone number. * phonetype The type of phone number (home, work, cell) * company The person's place of business. * comments Arbitrary notes associated with the person. * id A unique id by with to identify the contact record. * ================= */ function Contact (firstName, lastName, email, phone, phonetype, company, comments, id) { // Assign user-defined parameters to corresponding properties. this.firstName = (typeof firstName == "string") ? firstName : ""; this.lastName = (typeof lastName == "string") ? lastName : ""; this.email = (typeof email == "string") ? email : ""; this.phone = (typeof phone == "string") ? phone : ""; this.phonetype = (typeof phone == "string") ? phonetype : ""; this.company = (typeof phone == "string") ? company : ""; this.comments = (typeof phone == "string") ? comments : ""; // Note the creation time for this contact. this.dateAdded = this.dateModified = new Date(); // Assign the contact a unique id, provided by the addContact() // method of the ContactManager class. this.id = id; }
Note that the Contact and the ContactManager classes are stored in separate .as files named Contact.as
and ContactManager.as
. These files are loaded into our address book with the #include
directive as follows:
// Load data management classes #include "Contact.as" #include "ContactManager.as"
This code is placed on frame 1 of the scripts layer.
![]() ![]() ![]() |