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 Adding the Game Logic get owner() Solution

how is the owner property linked to this.token if owner isn't a class?

Hello, I don't understand how is linked the owner property in the owner getter. There is no owner class like to create a const owner = new Owner() or a const token = new Token(); like to call the owner attribute in Space.js;

get owner(){
  if(this.token === null){
    return null;
  }else{
    return this.token.owner;
  }
}

this.token was declared in the class Space, but owner is not part of this class, neither was created in the Space class.

I don't see the connection between both.

Thanks

Andrii Kodola
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrii Kodola
Full Stack JavaScript Techdegree Student 16,870 Points

Hi.

When Space first created it will have its property Space.token set to null. But then you can change this property by passing Token object to it. And token object has property Token.owner. So you won't try to access space.owner property or getter, but token.owner property.

In other words, this.token.owner means this.token = token placed inside this space, and this.token.owner = owner of the token placed inside this space.

Hope it will be useful.

2 Answers

Ross King
Ross King
22,165 Points

Once a Token object is assigned to a Space, you can then reference that Token by calling this.token, which will give you access to all of its properties (e.g., owner, id, dropped, columnLocation).

When you call this.token.owner, 'this' is referring to the Space object you have created, and the .token.owner part is directly referring to the Token object that has been placed inside of the Space's token property (this.token).

this.token + token.owner = this.token.owner

Does that help? :)

Well guys I thought I was trying to understand with the response above but I just wanted to confirm if the this.token.owner / / .owner has nothing related with the this.owner property inside the Token Class

Can somebody confirm this?

Thanks