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 trialMelinda Biber
4,124 Pointssetting an anonymous function as a method in another object
How to set the 'genericGreet' function on 'andrew' and 'ryan' as a 'greet' method?
var genericGreet = function() {
return "Hello, my name is " + this.name;
}
var andrew = {
name: "Andrew",
greet: genericGreet ()
}
var ryan = {
name: "Ryan",
greet: genericGreet ()
}
1 Answer
Dave McFarland
Treehouse TeacherYou can assign a function to an object property. For example:
function foo() {
}
var objectA = {
myFoo : foo
}
You can then call the function on the object (we now call the function a "method of the object") like this: objectA.foo()
Hopefully that's enough of a clue to get you through the challenge. If not, post back here and I can give you the answer.