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 Filling Out the Play Method

How to use the dot notation in this task?

Inside the play method, write an empty if statement that checks if it's the players turn. Use dot notation.

object.js
const player1 = {
    name: 'Ashley',
    color: 'purple',
    isTurn: true,
    play: function(){

    }
}

5 Answers

const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if (this.isTurn) { return player1['name'] + ' is now playing!' } } }

Thank you so much. :)

Hey thanks for the help on this it worked. But, I am kind of confused where the 'this' came from though. I don't remember anything about adding a 'this' before a dot notation.

Explanation for the 'this' keyword is mentioned in the Teachers Notes.

Like this!

const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(true) { return player1.name; } } }

Ah, that if statement is always going to be true.

You will want a statement that is only true when it's player1's turn.

It's doesn't work like this, too.

const player1 = { name: 'Ashley', color: 'purple', isTurn: true, play: function(){ if(isTurn) { return player1.name; } } }

Danny L.
Danny L.
10,837 Points

Hi Sumalee,

I know this post is a few months only but I figured I'd answer anyway.

Your code is almost there but it is missing the this keyword to specify where the isTurn is coming from. It should look like this:

const player1 = {
   name: 'Ashley',
   color: 'purple',
   isTurn: true,
   play: function () {
      //below is where you need to include the this keyword
      if(this.isTurn) {
         return player1.name + " is now playing!"
      }
   }
}

Hope that helps!

You're welcome, happy coding :)

player1 is an object.

To access a property in an object, you can use dot notation or bracket notation.

For example, if I wanted the name, I could type either player1.name (dot notation) or player1['name'] (bracket notation, note that it's a string within the brackets, this is because with bracket notation you can use variables and numbers as well, in fact ANY type of expression is allowed within brackets, even functions! Whereas dot notation ONLY allows strings)

Thank you so much for your help, but I still can't pass this task. :(

what did you try?