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

benjamin perodeau
benjamin perodeau
7,469 Points

objects

This is the toughest stage. Can't get this one right after many attempts. Please advise.

2 Answers

Chris Dziewa
Chris Dziewa
17,781 Points

So when you create a property in JavaScript you give each property a name (Same as a key) followed by a colon, followed by its value. If there are multiple values, separate each set with commas (except for the last one). You can create methods like this:

var andrew = {

  methodName : function (parameters) {
    //code block to run
    } 
};

You can also add the method to an existing object by using dot notation like this:

andrew.methodName = function(parameters) {
    //code block to run
} 

In the case of the challenge, the function and objects have been provided for you. All you have to do is add the pre-made function to the existing objects like this:

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

      var ryan = {
        name: "Ryan",
        greet: genericGreet
      }

A side note: To call these functions, use dot notation and add a set of parentheses and any necessary parameters: andrew.greet();

Chris Dziewa
Chris Dziewa
17,781 Points

No problem! JavaScript is a lot of fun, just keep at it!