"Team Stats" Project in "Javascript Arrays, Loops, and Objects", Question about "team.players.push(player)"

One question:

Why are we using "team.players.push(player);? Why aren’t we using "team._players.push(player);?

I would think we’re pushing this new property into the _players array, so why aren’t we using the underscore?

I know it has something to do with the getters we added (get players() {}:wink: and that it returns this._players. But why do we need this getter at all? Why not push directly into the array, without this seemingly unnecessary getter middleman?

Thanks for the help.

we do, the getter returns the underlying property.

but you don’t want to bypass your getter, if you bypass the getter now and then add validation or additional code to your getter, you risk introducing bugs

I’m unclear as to why we need the getter at all. Why doesn’t the addPlayer function simply use this._players.push(player), and no need to include a getter in our code.

Are object literals unique in some way that the addPlayer function can’t push a new player into the _players array without using a getter?

That is the exercise his problem, the getter seems to implement the default property behavior. Although getters give you more fine grained control, that isn’t utilized here.

Doesn’t mean getters aren’t useful.

I also think Javascript did a crappy job, even though we implement a getter, we can still access the underlying property directly (no proper encapsulation), which really sucks.

its just bad practice to bypass your getter when you have one. Once your code base starts to grow, this can become very confusing and introduce bugs and problems

You’re right, I’ve read that Javascript didn’t explain many things well. I was asking about the nature of needing a getter at all for object literals, since as you’ve just said, this exercise doesn’t seem to need the getter.

But I’m not asking about bypassing the getter, I’m asking why not just not include the getter? I’m guessing there are times when the property array cannot be accessed without the getter?

Thanks for getting back to me so quickly, by the way.

For this specific case, the getter seems to only increase complexity without any additional gains.

Or you could implement a getter, which validates the object you are trying to push? then getters and setters would actually be useful

I think this is also fundamental a problem in teaching programming, you want to introduce a concept (getter, function, classes, anything basically) but also keep it simple. This often to lead to questions like: why do we need this/it? And that the answer to this question is: Go build a “larger” project, you will see why these concepts are some useful.

Team stats project is what? 100 lines max in a single file? That is almost nothing. I work for months on the same projects, which means you easily end up with more then 100 files. Then code organization and using the right concepts becomes so important

Ah okay, thank you very much for the explanation. It’s not useful in this instance, but the concept will become useful when I do more complex things. Thanks!

Well, its useful to learn this concept (or any concept) without having an overly complex problem and then having to learn the concept