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 JavaScript Foundations Objects Methods

What is wrong

Stuck on this one. What am I doing wrong?

var genericGreet = function() { return "Hello, my name is " + this.name; } var andrew = { name: "Andrew", greet: andrew.genericGreet } var ryan = { name: "Ryan" greet: ryan.genericGreet }

3 Answers

Laura Cressman
Laura Cressman
12,548 Points

Hi Evelyn, When defining the variables' greet method, you should use this.genericGreet for both instead of the name, otherwise for example andrew.genericGreet will always return "Hello, my name is Andrew". This could be a problem if you change his name, for example. Here's what the code should look like for Andrew:

I'm sure you can figure out what to do with Ryan from here. Hope this helps!

 var genericGreet = function() {
        return "Hello, my name is " + this.name;
      }

      var andrew = {
        name: "Andrew",
        greet: this.genericGreet
      }

Hi Evelyn,

Three things:

First, you left out the comma after "Ryan", second, greet: andrew.genericGreet should be greet: genericGreet, and third, third is a lot like second, I'll bet you can figure it out.

Jeff

Thank you Laura and Jeff. I tried the suggestions that both of you provided(greet:genericGreet and greet:this.genericGreet) and both worked. Is one better to use than the other? I am guessing not but rather a preference. Can one of you clarify?

Thanks in advance