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 Building Constructor Methods and Generating Objects Token Properties Solution

Richard Eldridge
Richard Eldridge
8,229 Points

this.dropped = false?

Why is it not this.dropped = dropped and dropped = false in the constructor parameters? Is it functionally the same?

4 Answers

Steven Parker
Steven Parker
231,248 Points

The second example adds a third parameter. By setting to "false" by default, then when only two arguments are given it will perform the same as the first example. But by providing a third argument, the value of "this.dropped" can be set to something else.

Ignacio Martin Elias
Ignacio Martin Elias
10,127 Points

Here's the deal, you have 3 options.

Option 1 - you pass the 'dropped' property as a parameter and when you create an instance of that object you can set the value to true or false.

Option 2 - you don't pass the 'dropped' attribute as a parameter, in the constructor you set it to true or false and whenever you create an instance of that object it will always have the same value.

Option 3 - you pass the 'dropped' property as a parameter and inside the parenthesis you set it to a value ('dropped = true' or 'dropped = false'). When you create a new instance of that object you can use that argument or not. If you use it it will have the value you passed in as a parameter, but if you don't it will use the 'default value' (the value you put in the constructor method).

Hope it helps!

Steven Parker
Steven Parker
231,248 Points

The constructor is creating a new variable named "this.dropped". There is not any identifier named just "dropped" that could be used as the source of an assignment.

Also, assignment order is important. Even if an identifier named "dropped" were accessible, using it to assign "this.dropped" first would give it the value it had at that moment, which might not be "false".

Richard Eldridge
Richard Eldridge
8,229 Points

I'm not sure I understand. Is there a difference between:

class Token {
    constructor(index, owner){
        this.owner = owner;
        this.id = `token-${index}-${owner.id}`;
        this.dropped = false;
    }
}

and

class Token {
    constructor(index, owner, dropped = false){
        this.owner = owner;
        this.id = `token-${index}-${owner.id}`;
        this.dropped = dropped;
    }
}

?

This was how the "active" property was defined in the Player constructor.

Simon Feuster
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Simon Feuster
Full Stack JavaScript Techdegree Graduate 28,036 Points

I think there is no difference between the two examples, while running the code. But if you think about the game logic, then there is no need to choose if it should be true or false while you create the token. For this game you always want "dropped" to be false by creating the token. If you would set this.dropped = dropped and in the constructor dropped = false you would be able to change the default value to true. But that makes no sense, because dropped should only be true if the token had been used, not immediately after creating. So there is no need to choose a value in the constructor. dropped should always be false at the beginning.

In the first example there it is not possible for dropped to be anything else but false upon Token creation. The second example allows for whoever is creating the token to set dropped to anything they want. In the case of this game, you want to guarantee that no tokens are dropped since the game hasn't started yet.