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

Michael Criste
Michael Criste
5,045 Points

I left this lesson for a bit to study Ruby. Now, when coming back, I need a refresher as to how to structure this.

So, I'm inserting a function, which is set equal to a variable, and I'm putting it inside an object's profile. Please help with syntax. Thank you.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Objects</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Objects: Methods</h2>
    <script>


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

      var andrew = {
        name: "Andrew"
        greet: genericGreet(andrew);
      }

      var ryan = {
        name: "Ryan"
        greet: genericGreet(ryan);
      }

    </script>
  </body>
</html>

3 Answers

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,476 Points

Hi Michael

You don't need to pass an argument to the genericGreet methods; this.name will refer to the name property in each object. So, for example, if you were to call ryan.genericGreet, the return value would be "Hello, my name is Ryan".

Sean T. Unwin
Sean T. Unwin
28,660 Points

Hi Michael,

You need to add a comma after each item in an object, except the last item. Leave the end of the last item without punctuation on objects so remove the semi-colon.

The function does not take any parameters so you don't need to specify any.

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

Happy coding! :)

Michael Criste
Michael Criste
5,045 Points

Thank you! I guess getting comfortable with syntax takes repetition and practice. It was simpler than I had thought.