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: Challenge Making the Game Interactive playToken() Method Solution

Leonardo Motta
Leonardo Motta
11,284 Points

confused with game.ready

in this project what the game means here? we are adding this if statement in the playToken() method which belongs to the game object. Is game.ready a reference to the game object ready property? why don't we use this.ready? How javaScript knows game.ready is a reference to the game's ready property?

    if (targetSpace !== null) {
      game.ready = false;
      activeToken.drop(targetSpace);
    }

I read all of the code in that file, but I couldn't find any definitions of game. I am curious also :/

Ashley Boucher Guil Hernandez

Leonardo Motta
Leonardo Motta
11,284 Points

it was also not really clear for me, I tried to find definition for game but I did not find it, I am confused if it is a reference to the game object ready property (this.ready)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Leonardo Motta ! Yes, game.ready is a reference to the ready property on an instance of the Game class. The game object was declared on line 1 of app.js. That being said, I feel like you should be able to use this.ready here.

Hope this helps! :sparkles:

How does the file containing game.ready = false; access the game variable from app.js?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Alexander Davison the app.js is linked in the index.html. All those files before it are the set up of the classes, as you are well aware. But until the class that sort of kick starts this chain of events (the Game) object, is created nothing really happens. None of that code is executable by itself. It's all just in a state of waiting. The thing that sets this whole thing in motion is the creation of that instance of Game The game then creates the players and board. The board creates the spaces and tokens. The reason it can read that game variable is because it is now in the global namespace and none of these are modules so they can see each other.

Dzmitry Aliakseichyk
Dzmitry Aliakseichyk
12,290 Points

She made a mistake, people make mistakes. If you open the Final Complete Project Game.js she added a line const game = this; before game.ready.