Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,148 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);
}