Range error

So i’m working on the team stats js project.

Made the code but when I went to add a player into my array I received a range error.

I haven’t seen this before and I DID FIX MY PROBLEM! =]

BUT I don’t understand exactly what is going on.

HERE IS THE CODE:

const team = {
_players: ,
_games: ,
get _players() { // ORIGINAL CODE THAT PRODUCES RANGE ERROR
// FIXED CODE WITH: get players() {},
this._players;
},
get _games() { // SAME AS PLAYERS GETTER
// I FIXED THE ERROR WITH: get games() {},
this._games;
},
addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age,
};
this._players.push(player);
},
};

/*
team.addPlayer(‘Steph’, ‘Curry’, 28);
console.log(team._players);
*/

So my question is why does get _players() produce that range error?

How do you call a property of an object? And how do you call a getter?

console.log(object.propertyName) // calls the property of the object
console.log(object.getterName) // calls the getter

Because in the case of get _players, if you call team._players the console doesn’t know if you mean the getter (get _players) or the _players property of team since they have the same name. Same case with get _games and team._games..

Thats why your getters are setup like this:

get players() {
  this._players; // calls for the _players property of this object a.k.a. team
}

If you want a list printed of the team’s players you now call

console.log(team.players); // calls the getter, which in turn calls the property of team

Technically you could ditch the getter and call the property directly. But that is not advisable.

console.log(team._players) // calls the property _players of team

Hope this helps :wink:

Happy coding!

2 Likes

yesss thank you!

Ok I think I understand now.
So that would explain why about a dozen errors popped?
Because it would check back and forth between the two until a certain point and then give me the error?

1 Like

That and if you have it set up like this:

get _players() { 
  this._players;
}

It will keep calling back on it self causing too much recursion

1 Like

This is something I don’t really understand in the lessons. Because conditional statements are being used to check for truthy values.
So if I did this instead:

get _players() {
if ( this._players) {
return this._players;
} else {
console.log(‘Something went wrong’);
}
}
This would just check for the correct value?

1 Like

I am not sure what you mean. Yes you could implement an if statement but I don’t see why you would other than perhaps indicate that there are no players at all.

  get players() { 
    if (this._players) {
      return this._players;
    } else {
      console.log('there are no players in this team')
    }
  }

If need a bit more explanation on my previous post:

get _players() { 
  this._players; // will call this._players which is the getter ergo itself, thus a never ending loop will start, causing too much recursion.
}
2 Likes

Ok ok so:

get _players() { this._players; }

tries to access the _ players property.

&&

get players() { this._players;

tries to access the values within the _players property?

1 Like

Yes

Well, no, since the getter is named _players, this._players will call itself causing a recursive loop.

2 Likes

Thank you so much for your help! I’ve been waiting patiently for a response to this thread and couldn’t be more satisfied with the answers!

2 Likes

Sure anytime, happy coding!

1 Like

hey there, me again.

I’m going over JS from the beginning and it dawned on me that since ‘console’ is an object, we can access the properties of console right?

So >.< ='D I logged console.log(console) and found that interesting and remembered I could do something like this : console.log(console.log). Which cracks me up.

So I have a new question, if you don’t mind, how can I print more info about the console object properties? More specifically is it possible to log the individual functions code?

I am not sure what you would like to achieve, but more info on the console object can be found here:
https://www.w3schools.com/jsref/obj_console.asp