Explanation

You're probably overwhelmed by all this new code so we'll look at it in chunks.

Overview

function Person(firstName, lastName, gender, age, birthday) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = gender;
    this.age = age;
    this.birthday = birthday;
    this.nextYear = this.age + 1;
    this.fullName = function () {
        return (this.firstName + " " + this.lastName);
    };
}

We've used what's called a constructor function to create our Person object. You'll notice the properties listed both in parentheses and within the function as well. We include the this keyword because we want to refer to the property's value for that specific object. As you'll see later, this constructor function will be used as a basis for other objects we create. They'll be able to use its properties simply by referencing the constructor function.

Additional Functions

 this.pronoun = function () {
        if (this.gender.toLowerCase() === 'male') {
            return "He";
        } else if (this.gender.toLowerCase() === 'female') {
            return "She";
        } else {
            return "They";
        }
    };
    this.possessive = function () {
        if (this.gender.toLowerCase() === 'male') {
            return "his";
        } else if (this.gender.toLowerCase() === 'female') {
            return "her";
        } else {
            return "their";
        }
    };
    this.ordinalSuffix = function (i) {
        var j = i % 10,
            k = i % 100;
        if (j == 1 && k != 11) {
            return i + "st";
        }
        if (j == 2 && k != 12) {
            return i + "nd";
        }
        if (j == 3 && k != 13) {
            return i + "rd";
        }
        return i + "th";
    };

We also need to declare a few more functions that will be properties as well to a certain degree. 1. pronoun() - based on the value of the gender property, we return a pronoun of He for "male", She for "female", or They for anything else. 2. possessive() - we also have a function to cover possessives. It returns either his, her, or their respectively. 3. ordinalSuffix() - When notating order with numbers (i.e. 1st, 2nd, 3rd, etc.), the extra bit tacked onto the number is called an ordinal suffix. We've used a function to assign an ordinal suffix based on the remainder. With the exceptions of the -teens (11-19), ordinal suffixes work like so:

  • Any number that ends in a 1 has an ordinal suffix of st. For example, First, 51st, etc.

  • Any number that ends in a 2 has an ordinal suffix of nd. For example, Second, 92nd, etc.

  • Any number that ends in a 3 has an ordinal suffix of rd. For example, Third, 33rd, etc.

  • The -teens and all other ending digits have an ordinal suffix of th. For example, Fifteenth, 0th, 513th, etc.

Outputting the results

var person1 = new Person('Bob', 'Smith', 'male', 35, 'March 4');
console.log(person1);
console.log(person1.fullName());
console.log(person1.pronoun());
console.log(person1.nextYear);
console.log(person1.ordinalSuffix(person1.age));

In order to display our results we need to define an instance of our Person object. This is where we define a variable to hold the instance and provide values for the properties. Once we've defined an instance, we can log it to the console. You may see in the output that all of functions are displayed as [Function]. In order to check the output of them, we log them individually. For example, console.log(person1.pronoun()) gives us the result of the pronoun() function for person1, which is He. It's important to remember to include the parentheses after the function name. If your function takes in a certain value, such as ordinalSuffix() does, include it inside those parentheses. In this example, we gave it person1.age which would allow us to output something like "Tomorrow is Johnny's 15th birthday".

Last updated