Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

brandonlind2
brandonlind2
7,823 Points

how do you pass an object property as an argument?

for example how would I pass the names of person1 and person2 into the persons function?

function person(age,job,name){ this.age=age; this.name=name; } var person1= new person(24, 'beth'); var person2= new person(30, 'sam');

function persons(person1 ,person2){ console.log(person1 + ' and ' + person2) }

I'm getting a missing after argument syntax error when I type it like

function persons(person1.name,person2.name){ console.log(person1 + ' and ' + person2) }

1 Answer

Steven Parker
Steven Parker
230,274 Points

You can't define a function like that. You might call one giving it specific object properties, but when you define it, you give pure variable names that represent the arguments that will be passed in when called.

Now you could extract properties from the arguments once inside the function, like this:

function persons(person1, person2) {
  console.log(person1.name + ' and ' + person2.name);
}