28 Passing Objects into Functions


#1

Just to be clear I am not actually stuck but I was hoping someone could clear this up for me. How does the arguments person1 and person2 become objects of the Person constructor without having to define them?

Any answers would be greatly appreciated.

// 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;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);


#2

here do you create the object:

function Person (name, age) {
    this.name = name;
    this.age = age;
}

then you create instances of your object here:

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

so now when you would call age difference:

ageDifference(alice,billy);

the arguments (alice and billy) are stored in the function parameters (person1 and person2)

the confusing part of your code (or what might cause confusing) is that the function calls are missing


#3

The order in which the was written confused me, thank you for clarifying this for me.


#4

do you understand it now?


#5

Yes I understand now.


#6