JavaScript: Review of Objects and Constructors

The next section on codeacademy is called Arrays of Objects, in which I would infer I’m about to create arrays that contain objects. Let’s see if I’m right.

A bit of useful information
An object is seen as just another type of data, such as a string or a number. Anything that you are able to do with other types of data, you can also do with objects. This means you can create arrays of objects.

Creating an Array of Objects

In my previous post about creating custom constructors, we created one called Person which appeared in a few examples. We’re going to continue using this constructor, but this time we get to create an array which contains the Objects we create.

// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

Creating a loop to run through an array and retrieve data from the objects within.

An array containing objects can be looped through just like any other array containing other types of data.

So the first mission is to create a for loop that will print out the name property of each object in the array. We can use a basic for loop which declares a variable and sets it to 0, we’ll keep the loop running until i <=3, and then we’ll increment i by 1 every time the loop runs.

for(var i = 0; i <= 3; i++ )
{
    console.log(family[i].name);

}

family[i] let’s us move through the arrays values as the loop increments i and the .name tells it to retrieve value that the name property contains.

Passing Objects into Functions

It looks like our versatility with Objects continues to grow, objects can also be used as parameters for functions! This is useful because it allows for functions to utilize the methods and properties the objects provide.

The following example has the Person constructor and creates a new function that calculates the age difference between the two people entered as parameters.

// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
    return person1.age - person2.age;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice,billy);

The objects Alice and Billy are created using the constructor. Remember: Objects are just a type of data like a number or string value. This means we can pass Alice and Billy in as parameters when we call our function to calculate the age difference. Because the properties are still associated with the object when passed into the function, we can specify which property we want to pull data from.

Another example of creating a function and passing in objects as parameters. The following function, appended to the previous example, will return the age of the whoever is oldest.

//Create a function called olderAge
//It will take 2 parameters
var olderAge = function(person1, person2) {
    if(person1.age > person2.age) {
        return person1.age;
    }
    else{
        return person2.age;
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s