last day (15 days later) » 

18:31
0
Q: Closure Instance Undefined

PositiveGuyTrying to figure out how to add a new player now that I've converted my ES6 module to use a closure. So I've created a closure via factory basically: Player.js - now a closure / factory function Player(){ let players = [], player = this; const PlayerType = Object.freeze({ COMPUTER: "C...

Well where is player declared/defined? — oh well that's just a local variable in the constructor. It won't be visible anywhere else.
Is addPlayer inside the Player function?
at the top of my Player.js closure. I stated above "let players = [], player = this;" that I added player = this next to the players array
Right, so player is visible inside the Player function but not outside. (Note that it's not at all clear whether addPlayer() is some separate function or is intended to be part of the prototype, or what.
In "normal" function calls foo(), this is undefined. It's unclear to me what you expect the value of this to be here.
18:31
sorry yes it's inside the player function @JonasW.
@FelixKling hmm not sure how to work with the instance then in this case
There is no instance. If you call the function with new, then this would be an "instance" of the (constructor) function. But I don't see how that makes sense anyway. It looks like addPlayer should always return a new object (btw, Object.assign({}, {...}) is the same as just {...}).
The thing that's really confusing is that you've split the code up, making it really unclear how one part relates to another.
when you call the function it creates a new scope and new set of closures, that's what I mean by "instance"
What is the purpose of the Player function anyway? It doesn't seem to represent a single player so the name is already confusion. Should it be called Game instead maybe?
@Pointy ok let me update the post to try to make it more clear
18:31
@PositiveGuy: OK, but this is not the scope. You cannot access scope from user-land code. this is like an implicitly set parameter.
@FelixKling"What is the purpose of the Player function anyway? " I want closures so I can create separate instances when I need to.
This might be a good use case for a class then.
"Trying to figure out how to work with that player = Player(); instance of player by calling the add function to add to the array of that player instance" Can you elaborate on that an provide a clearer and more complete example?
Your previous version of the addPlayer function looked alright. You were creating a new player from an empty object and pusing it into to the players array. Why would you want to create a new player from any kind of existing instance?
I've updated my post sorry for it being confusing.
crap thanks for pointing out the obvious @FelixKling. But when I add it via {} that player in the array it's not inheriting all the functions I'm returning...that's my problem with that route. Because creating an instance off an empty object is totally separate from the player instance I'm working with
"it's not inheriting all the functions I'm returning" which functions? Inheriting from where? Please provide a Minimal, Complete, and Verifiable example. I think what makes it difficult for us to help you is that you are not providing the complete picture.
18:31
return { PlayerType, players, getPlayerById };
I need my added players to be of the same instance of the Player() I'm working with
I find it confusing that a player contains players. That makes no sense to me. However, if you want the "inner objects" to have the same method as the one you return from Player then just set those: const newPlayer = {id: null, name, symbol, type: playerType, move: null, PlayerType, players, getPlayerById};. But overall this looks like a bad solution to me. You should describe the high level architecture / goal so we can understand better what you want to achieve.
"I need my added players to be of the same instance of the Player()". The Player() you're working with is an object with 4 function references who have access to the players array through a closure. Your added players are plain objects with a few properties : id, name, move, symbol, etc. This is your confusion. They can't be the same instance. It doesn't make sense.
@GuillaumeGeorges I know that's the problem I was using {} but didn't know a way to change that function to work with the currently scoped Player() in order to add new Player()s to the player array

last day (15 days later) »