Where is person1 & person2 defined?


#1


https://www.codecademy.com/en/courses/spencer-sandbox/4/4?curriculum_id=506324b3a7dffd00020bf661


This code works. My question is about how it works. Where in this example is person1 & person 2 defined?
The function Person has a capital 'P'. Also it does not have a number e.g. (person'1'). How does javascript know when we declare person1 / person2 to relate it to the Person function capital 'P' no number?


// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
    return person1.age - person2.age;
};

// Make a new function, olderAge, to return the age of
// the older of two people
function olderAge (person1, person2){
   if(person1.age > person2.age){
       return person1.age}
       else{return person2.age}}


// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

console.log("The older person is " + olderAge(alice, billy));


#2

person1 and person2 are function parameters, they are placholders until you pass actually values when calling the function:

olderAge(alice, billy)

now person1 gets assigned alice, and person2 gets assigned billy


#3

Oh ok I think I got it. So the function is being called inside the console.log? If so that makes sense. Thanks!

console.log("The older person is " + olderAge(alice, billy));

#4

yes, and when you call the function, you pass arguments, which are then assigned to the function parameters.


#5

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