Explanation

Let's look at the code in chunks

Constructor Function

function Parent(firstName, lastName, gender, age, birthday, isMarried, occupation) {
    Person.call(this, firstName, lastName, gender, age, birthday);

    this.isMarried = isMarried;
    this.occupation = occupation;
}

This looks similar to our Person() function, but with a few differences. 1. Two new properties

  • isMarried - a boolean that states whether or not the parent is married.

  • occupation - a string that stores the parent's job.

    1. Person.call()

  • We need to call on the properties already defined by the Person object. It is crucial that you also include the this keyword. That way we can use all these properties.

Prototype

Parent.prototype = Object.create(Person.prototype);
Parent.prototype.constructor = Parent;

In this bit of code, we create the Parent constructor function based on the Person constructor function. Parent uses Person as a template or prototype.

Job Function

Parent.prototype.job = function () {
    if (this.pronoun().toLowerCase() == "he" || this.pronoun().toLowerCase() == "she") {
        return (this.pronoun() + " is a/an " + this.occupation + ".");
    } else {
        return (this.pronoun() + " are a/an " + this.occupation + ".");
    }
};

Here, we create a function to primarily output Parent's occupation property. 1. We create the function on the constructor's prototype property.

  1. We use an if/else statement to output either is or are based on Parent's gender pronoun.

Displaying the Results

var parent1 = new Parent ('Jane', 'Jovi', 'female', 29, 'May 9', true, 'coder');
console.log(parent1);
console.log(parent1.job());

Here's where we put it all together. 1. We create a new instance of the Parent object. 2. We log data to the console. Functions need to be logged separately because they aren't technically properties.

Last updated