Our address book is great at keeping track of our contacts' full names; but conspicuously enough, it's missing any actual addresses! Let's add this functionality now.

If you think about it, addresses have properties of their own. They have a street, a city, a state, and a zip code. Because every address will have these same properties, let's create Address objects. Then, we'll associate these Address objects with the Contact object to which the address belongs. When we're done, we'll have a Contact object with an addresses property. This addresses property will be an array that eventually contains multipleAddress objects.

In our app, we want to associate multipleAddress objects to each of our Contact objects, since a user may have a personal address, a work or business address, a mailing address, etc. that are all different.

Business Logic - Contact & Address Constructors

But now we want each Contact to contain not only a firstName and lastName, but an array of Address objects too. When we first create a new Contact, this array will start out empty. Then, when the user provides addresses for the particular Contact, we will create a new Address out of each, and push it to that Contact's addresses array.

To do this, let's make sure each new Contact object is initialize it with an empty addresses array. We'll add the following to the Contact constructor:

You may have noticed we did not pass in an empty array as an argument when we created our lisa object in the line var lisa = new Contact("Lisa", "Simpson");. Since we're defining the addresses property as the same thing every single time we create a new Contact, there is no reason to require an empty array as an argument. Instead, it's much more efficient to have our constructor automatically create an addresses property and assign it to be an empty array.

Now we need to write an Address constructor. At a minimum each Address object will need to know its own street, city, and state. We can create an Address constructor that will add each of those properties to every Address object created.

This should look fairly similar to our Contact constructor. When our Address constructor is called to create a new Address object, we will provide it parameters for a street, city and state. It will then set corresponding street, city and state properties on the Address object it's creating as equivalent to the arguments that were passed in.