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

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,723 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,690 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.