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

Add an empty method to the object literal called play(). WHat do you mean? player1.play()???

Do you want to insert an method to the object?

object.js
const player1 = {
  name: 'Jip',
  color: 'GreyishBrown',
  isTurn: true

}

player1.play()

10 Answers

Andreas Nyström
Andreas Nyström
8,887 Points

Hi.

To do this you want to put the function inside the object literal. With the property of play and then a function as value. If you don't get it after you complete it, rewatch the video again.

const player1 = {
  name: 'NameHere',
  color: 'ColorHere',
  isTurn: true,
  play: function(){}
}
Seth Johnson
Seth Johnson
15,200 Points

The wording of these instructions isn't very well done, or it's a deliberate misdirection, I'm not quite sure which. Also, apparently trying to put an arrow function (which I thought was encouraged as per all of the ES2015 videos?!) doesn't work either. : /

play: function(){}

That does the trick! The brackets after "play" in the question made me a little confused at first. After rewatching the video, I realized that she said, "The property key is the name of the method, and that's the name you'll use when you want to call it."

In the end, one is able to create a method, e.g., play(), by using the method's name as an object property.

I found this quite difficult as well. The instructions were a little misleading for a student.

Inside the object literal add the play method

const player1 = {
  name: 'Jip',
  color: 'GreyishBrown',
  isTurn: true,
//play method 

}
player1.play=" ";

Yes, the instructor poorly conveyed the following detail of creating a empty method ---> and directing it to an OBJECT LITERAL named PLAY()

This is correct way of creating your object literal and assigning it to a variable player1.

var player1 = {

name: 'Walter Driver', color: 'Green', isTurn: true,

play: function(){} // This portion was poorly explained and the question was not as clear on what it was asking }

Jasper Kop
Jasper Kop
10,423 Points

how could I know the answer for this question?

const player1 = { name : 'abhinav', color: 'Dark', isTurn: true, play: function (player1) {}

}

const player1 = { name: 'rana', color: 'brown', isTurn: true, play: function() { } }