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

Stuck on quiz but I think my code is correct.

Stuck on quiz but I think my code is correct. Is this a bug?

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

1 Answer

andren
andren
28,558 Points

Your code would technically work, but it is not ideal for two reasons:

  1. It is bad form to reference the name of the variable the object is stored inside of inside of the object. This is due to the fact that while the code would work in this specific case, if the object was assigned to a variable or parameter with a different name then bugs could occur.
  2. isTurn contains a Boolean so comparing it to true is pointless. The comparison will result in the exact same value already stored in the variable.

The first issue can be solved by using the this keyword, and the second by just not using a comparison at all. Like this:

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

With those changes your code will be accepted by the code checker.

Thank you andren!