Javascript - Team Stats


Hi guys,

I was just wondering at line 47 & 50, should I use .players or ._players and games or _games respectively?

I think both create the same result.

Also, does get method creates a new object? I don’t understand why players and games are an object as I never create it, but it works on line 47 & 50.

Thank you guys!!

1 Like

besides maybe creating the same result, you need to understand how this result is generated.

Just to see how your understanding is (before i go into explaining), can you tell me what getters are? What could they be used for? What purpose do they serve?

1 Like

Thanks for your time!

My understanding for getters are, they are kind of a “Beacon” that points to the space of memory in a specific location. For example, get games() is pointing to _games object, that’s why I don’t understand on line 47 & 50, how come you can push an object into players & games?

I think getters should only be used when you don’t want to modify the original object, in this case _players & _games objects.

But from the code in the exercise, it seems like getters method are creating a new object??

_games isn’t an object, its a property of an object. team is an object.

The getter returns the underlying property, so doing will return the property

Have setters been covered yet? Judging by the fact that methods are used, i would say not. Setters allows us to change the default behavior, normally doing:

obj.someProperty = 'new value'

would overwrite the property with a new string value, however if we implemented a setter:

obj = {
    this._someProperty = [];
    set someProperty(valIn)

now, we assign a new value, but the value is pushed into the array. We can then use the setter also to do validation if we want

and then we could use the getter to print a nice string representation. Which makes getters and setters a lot more powerful