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 Object-Oriented JavaScript Object Basics Creating Object Literals

Rhonda Goolsby
Rhonda Goolsby
6,992 Points

I do not understand how to add an empty method to an object literal called play() to this exercise.

Am I suppose to add an new object literal?

object.js
const player1 = {
name: 'Rhonda',
color: 'pink',
isTurn: true,
}

2 Answers

Eric M
Eric M
11,545 Points

Hi Rhonda,

This challenge is asking you to modify the existing object literal, buy adding an empty method.

An empty function outside of an object might look like this:

function my_function()
{
        /* nothing going on here */
}

or this

function my_function() {}

or even this

let my_function = () => {}

In an object methods are stored in a similar way to other properties.

const some_object = {
        prop_one: "value";
        my_method: function() {}
}

If I wanted that method to do something I'd need to fill out the curley braces. Just like with all of the other empty functions above.

Best of luck!

Eric

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Ok, so all you need to do is remember how you define functions in JavaScript like so:

function name() {

}

However, we can also create a function through variables like this:

let name = function() {
}

Well in a method the attribute is like a variable but we remove the let/var/const identifier, and replace the = with a :

so we are left with this:

name: function() {
}

Hopefully this should be enough information for you to solve the challenge!

Eric M
Eric M
11,545 Points

Ha, good answer yourself! I always find it's helpful to have the same concept explained from different perspectives, so hopefully Rhonda gets something from having both of these explanations!