Passing arguments to the constructor

While it is all well and good for a father to hope that the baby looks like him, there is a chance that later you might want a baby with different features. What we need is the ability to create the baby with different attributes. These attributes can be assigned to the baby object from the parameter list, and passed to the baby function through arguments to the new Baby() method. In a true object-oriented language, the function that creates the object is called the constructor function. When an object is created using the new method, it is the constructor function that is called to create the object. Here is an example of a JavaScript constructor:

function Baby(babySex, hairColour, eyeColour, babyName)
{
this.sex = babySex;
this.hair = hairColour;
this.eyes = eyeColour;
this.name = babyName;
}

Now we can create as many babies with different attributes as we want. For example, we could create a baby girl named Alex using the following code:

var b2 = new Baby("Female", "Blonde", "Brown", "Alex");