Objects as properties of other objects

Finally, you can create objects that contain other objects. For example, to extend the previous example, we will assume that we have defined mother and father objects like we defined the baby object. We could then create a family object that would group together the mother, father and baby objects. The function would look like this:

function Family(mother, father, baby)
{
this.mum = mother;
this.dad = father;
this.baby = baby;
}

If we had a father object called Travis, a mother object called Kathryn and a baby object called Alex, we could create a family object using the following code:

var SimonFamily = new Family(Kathryn, Travis, Alex);

We can then access the methods and properties of the objects using the dot notation. If we wanted to make the baby object eat, we could use the following syntax:

SimonFamily.baby.eat();

Similarly, if we wanted to make another baby cry, we would use the syntax:

WhiteFamily.baby.cry();