Why doesn't 'this.person' work in lesson 3?


#1


https://www.codecademy.com/courses/building-an-address-book/0/3?curriculum_id=506324b3a7dffd00020bf661#

Hey,

I actually managed to get the code for this lesson working correctly in the end, however was curious why my first attempt (using this.firstName etc as opposed to person.firstName) prints 'undefined' 6 times.

In my mind applying logic from the previous chapter seemed to suggest it would work, but clearly this is not the case! Just curious as to why.

Many thanks.


var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

// printPerson added here
var printPerson = function (person) {
    console.log(this.firstName + " " + this.lastName);

};

printPerson(contacts[0]);
printPerson(contacts[1]);


#2

Arguments are referred to by name.

Constructors and methods get a reference to the object that they are creating or are called on:

var Ball = function(color) {
  this.color = color;
};

Ball.prototype.turnRed = function() {
  this.color = 'Red';
};

var myBall = new Ball('Blue');
console.log(myBall.color);  // Blue
myBall.turnRed();
console.log(myBall.color);  // Red