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 createTokens() Method Solution

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

createTokens(num)

Teacher Ashley said: Note how when I call the create tokens method, I'm using the "this" keyword.

1:42 Indicating that the method I want to call is available on the object that we're

1:46 initializing


question: if we have more than one method initialized on the constructor, do I have to be more specific with the call?
we have: constructor(name, id, color, active = false) { this.name = name; this.id = id; this.color = color; this.active = active; this.tokens = this.createTokens(21); this.otherStuff = this.OStuff(other); // this is the method added to create the question } do I have to call : for (let i = 0; i < num; i++) { let token = new Token(i, this.createTokens(21)); tokens.push(token); }

in order to make a clear difference between this two objects?

1 Answer

Richard Rowland
Richard Rowland
16,574 Points

No, because "this" refers to the specific object instance that the constructor creates, in this case a new player object. In your example;

this.tokens = this.createTokens(21);
this.otherStuff = this.OStuff(other);

"this" refers to the same object, that is the player object being created, not two different objects.