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
Bob Smith
6,711 PointsCode Challeng: Methods
I can't figure out from the video how to set the greet method as the genericGreet function. I am completely stumped. Code so far:
var genericGreet = function() { return "Hello, my name is " + this.name; }
var andrew = {
name: "Andrew",
genericGreet: andrew.name;
}
var ryan = {
name: "Ryan",
genericGreet: ryan.name;
}
4 Answers
khcir .
Courses Plus Student 8,527 Pointsi cant figure out neither, but somehow i fixed it, so here is the code...
/comment
var andrew = {
name: "Andrew",
greet: genericGreet
}
var ryan = {
name: "Ryan",
greet: genericGreet
}
Ryan Harper
3,441 PointsAfter banging my head...
var genericGreet = function() { return "Hello, my name is " + this.name; };
var andrew = {
name: "Andrew",
greet: this.genericGreet
};
var ryan = {
name: "Ryan",
greet: this.genericGreet
};
Milagro Scardigno
4,494 PointsThanks!
Luke Lee
7,577 PointsCan anyone tell me why the code doesn't work if I put ; after greet: genericGreet??
Thanks
Blaise Gratton
30,213 PointsDid you put commas at the end of the previous lines?
Bob Smith
6,711 PointsBob Smith
6,711 PointsThank you so much! However, if something could possibly explain to me how this works? I'm slightly confused.
Connor Nesbitt
4,824 PointsConnor Nesbitt
4,824 Pointsfrom what I can tell, it substitutes the function name, genericGreet, in for the actual function you would need to put into greet. it does not take a dot notation, such as andrew.name, since it asked you for the function not the name. So, since the question it asks is for you to put the function in for greet. and that function was already written and given a name. All you have to do is put the name of the function into greet for both ryan and andrew and the code will do the rest.
Luke Callahan
7,220 PointsLuke Callahan
7,220 PointsYeah, it's because the variable genericGreet has the function you want to run stored in itself. So, to call the function, we can just put the variable genericGreet as the 'value' in the key-value pair of greet: genericGreet.