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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![victor guia](https://uploads.teamtreehouse.com/production/profile-photos/1294772/micro_172047_1439596090205_1842142897_875531_6243651_o.jpg)
victor guia
8,098 PointsHelp on Javascript Objects Code Challenge
Hello,
I am having trouble with this code challenge. Below is my solution which returns an error:
var genericGreet = function() {
return "Hello, my name is " + this.name;
}
var andrew = {
name: "Andrew",
greet: genericGreet()
};
var ryan = {
name: "Ryan",
greet: andrew.greet
};
Hoping someone can help me. Thank you.
4 Answers
![Jason Anello](https://secure.gravatar.com/avatar/d1304e75238ff7f216f55adc03e08ea1?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Jason Anello
Courses Plus Student 94,610 PointsHi Victor,
You don't want to call the genericGreet function with the parentheses. You simply want to set the greet method to that function.
var genericGreet = function() {
return "Hello, my name is " + this.name;
}
var andrew = {
name: "Andrew",
greet: genericGreet
}
var ryan = {
name: "Ryan",
greet: genericGreet
}
![Michael Hinrichs](https://secure.gravatar.com/avatar/c89d551b8a11eb30476b27e4f312b471?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Michael Hinrichs
10,920 PointsHey Victor, what is the error?
![victor guia](https://uploads.teamtreehouse.com/production/profile-photos/1294772/micro_172047_1439596090205_1842142897_875531_6243651_o.jpg)
victor guia
8,098 PointsThanks Jason. That was the correct solution. But shouldn't we always call a function with ()?
![Jason Anello](https://secure.gravatar.com/avatar/d1304e75238ff7f216f55adc03e08ea1?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Jason Anello
Courses Plus Student 94,610 PointsYes, you would use () whenever you want to call and have a function executed.
But at that point all it wants is for you to assign the function to the greet
method. You don't want the function to be called and executed at that point.
You can later call the method like this: andrew.greet()
Here I have used the () because I want to call this method now and have it executed.
If you do use () where you tried then the function will be executed when that code is reached and the return value of that function is what will be assigned to greet
. So instead of greet
being a method that stores a function it will be a property that stores a string since that function returns a string.
![victor guia](https://uploads.teamtreehouse.com/production/profile-photos/1294772/micro_172047_1439596090205_1842142897_875531_6243651_o.jpg)
victor guia
8,098 PointsOk got it. Thanks so much.