It’s a bit of a follow up of this question but no need to read back. As that one is marked solved, I’m starting a new topic.
Not pasting the whole code, but making a pseudo example here to isolate the issue.
I cooked up something like this:
I have 2 classes. Grid and Gpoint. Gpoint is only instantiated by Grid’s make_Gpoint() method. Grid is instantiated once in the main sketch.
Ok. Now I need Gpoint to have an update func, that needs some data (changeable data. Should I say: a state?) from the Grid instance. No problem, as Grid is making the instances I just added the instance to the constructor, I tried:
(main sketch)
//in setup
grid = new Grid(/*blah*/)
gp = grid.make_Gpoint(x, y)
//in draw
gp.update(x,y);
(in Grid)
make_Gpoint(){
//blah
const gp = new Gpoint(x, y, this);
points.push(gp);
return gp;
}
(in Gpoint)
constructor(/*blah*/, grid){
//blah
this.grid = grid
}
//other stuff//
update(x, y){
//some math involving
this.grid.someProp
// I get here "can't access propety xyz of undefined."
}
So I read about this key word in js. But got confused about how to use bind(), call() or apply() in this case. To solve that. How?
For now I’m passing the instance as a parameter in draw:
//in draw
gp.update(x,y, grid);
And thats ok. It’s working fine. But… as I’m passing the caller (grid.make()) i thing it should not be necessary to pass it.
So how could I make the right bind. The literature is mostly based in functions as a class syntax (prior es6), and I’m not quite sure how I should use the binding functions in this scenario.
Or, perhaps, after all I should make Gpoint extends Grid?
thanks for reading : )