Lesson Weekend

As mentioned in the last lesson, a real world application would use a database. In a database, each individual Contact would have a unique ID. This allows us to identify records by a unique ID instead of something like their last name, which isn't guaranteed to be unique.

Unique Object IDs

Let's update our code so each Contact is assigned an ID as soon as it's created. It'll also come in handy when we later want to retrieve specific contacts in our application.

We'll update several functions to make this work. First, let's update the AddressBook constructor to instantiate new AddressBooks with a currentId property:

scripts.js
// Business Logic for AddressBook ---------
function AddressBook() {
  this.contacts = [],
  this.currentId = 0
}

...

Now each time a new AddressBook is created, it will have a currentId property that begins at 0.

Using this number as a reference, we can ensure each new Contact added to the AddressBook has a unique ID. We'll do this by defining another prototype called assignId():

scripts.js
// Business Logic for AddressBook ---------
function AddressBook() {
  this.contacts = []
  this.currentId = 0
}

AddressBook.prototype.addContact = function(contact) {
  this.contacts.push(contact);
}

AddressBook.prototype.assignId = function() {
  this.currentId += 1;
  return this.currentId;
}

...

This new method will increment the this.currentId property on the AddressBook object by 1 and return the updated value. This mimics a database by creating sequentially incrementing ID values which are never repeated (making them unique).

Finally, we need to call this new assignId() method whenever we add a new Contact to the AddressBook. We already have a method called addContact() that handles this. We'll just add the following code to it:

scripts.js
// Business Logic for AddressBook ---------

...

AddressBook.prototype.addContact = function(contact) {
  contact.id = this.assignId();   // <--- This line is new!
  this.contacts.push(contact);
}

...

This creates an id property on a Contact object, and assigns it a unique ID, incrementing value before pushing it to the contacts array in AddressBook.

In the real world we'd make a method like this more robust. For example, we'd make sure contacts that already have an ID don't get a new value assigned to their id property. However, this is fine for now! We'll explore best practices for IDs in further detail later on, when we learn databases.

After following along, the updated scripts.js file looks like this:

scripts.js
// Business Logic for AddressBook ---------
function AddressBook() {
  this.contacts = [],
  this.currentId = 0
}

AddressBook.prototype.addContact = function(contact) {
  contact.id = this.assignId();
  this.contacts.push(contact);
}

AddressBook.prototype.assignId = function() {
  this.currentId += 1;
  return this.currentId;
}

// Business Logic for Contacts ---------
function Contact(firstName, lastName, phoneNumber) {
  this.firstName = firstName,
  this.lastName = lastName,
  this.phoneNumber = phoneNumber
}

Contact.prototype.fullName = function() {
  return this.firstName + " " + this.lastName;
}

We can now create a Contact and add it to our AddressBook "database". When we do so, it will have a unique ID. It doesn't have the complexity or efficiency of a real database, but soon we'll be able to use our AddressBook to retrieve specific contacts just as we would with an actual database.


Example GitHub Repo for the Address Book

Example


After following along with this lesson, scripts.js looks like this:

scripts.js
// Business Logic for AddressBook ---------
function AddressBook() {
  this.contacts = [],
  this.currentId = 0
}

AddressBook.prototype.addContact = function(contact) {
  contact.id = this.assignId();
  this.contacts.push(contact);
}

AddressBook.prototype.assignId = function() {
  this.currentId += 1;
  return this.currentId;
}

// Business Logic for Contacts ---------
function Contact(firstName, lastName, phoneNumber) {
  this.firstName = firstName,
  this.lastName = lastName,
  this.phoneNumber = phoneNumber
}

Contact.prototype.fullName = function() {
  return this.firstName + " " + this.lastName;
}

Example GitHub Repo for the Address Book