JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  • Constructor Function
  • Prototype
  • Job Function
  • Displaying the Results
  1. 04-Objects
  2. Solutions
  3. Inheritance Challenge

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.

PreviousCodeNextFamily Challenge

Last updated 7 years ago