How would I call a method in instanced object, but in context of another instanced object.
Best way to explain is with this brief expamle..
game.js
var ball = new Ball();
ball.addMotionListener(function(){this.color = someNewRandomColor});
inside ball.js
Ball = class{
constructor(){
this._position = new Vector(0,0);
this._color = red;
this._onChangeManager = new OnChangeManager();
}
addMotionListener(listener){
this._onChnangeManager.addMotionListner(listener);
}
set position(newP){
this._position = newP;
this._onChangeManager.motionEvent();
}
}
and in onChangeManager.js
OnChangeManager = class{
constructor{ this._motionListenerQueue = [] }
addMotionListener(newListener){
this._motionListenerQueue.push(newListener);
}
motionEvent(){
for(listener in _motionListenerQueue){
listener();
}
}
}
Now in game.js
kickBall(ball);
which changes ball position, triggers motion event listener in onChangeManager, with goal of changing balls color. Ofcourse this doesn't work, since this.color
is in ball's context and not in onChangeManager's.
Is it possible to run method in onChangeManager object, but in ball's context?
Edit: Sorry for trivial/duplicate question, I am not familiar with js contexts