Person parameter


#1

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

The code is correct, but I don’t understand what exactly the "person" parameter does. Is it some kind of universal parameter?
Why can’t I use "this.firstName" instead of "person.firstName"?


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
function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
};

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


#2

I don’t understand how the person parameter knows it is connected to mary and bob.


#3

printPerson is just a function, this is for objects/classes.

You already used parameters and arguments many times before? You can always check earlier lessons/documentation if you need a reminder

Short version, a function parameter (person in this case) serves as a placeholder, until you supply an argument when calling the function:

# function declaration with person parameter
function printPerson(person) {
     console.log(person)
}

# function call, supplying bob and mary as arguments
printPerson(contacts[0]);
printPerson(contacts[1]);

the arguments now get copied into the function parameter


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.