I have an app with the following structure (simplified a bit here)...
var RecorderApp = function(canvas) {
this.state = undefined;
this.states = { record: recordState, save: saveState };
this.init = function() {
this.audEng = new AudioEngine();
this.waveDisp = new WaveformDisplay(canvas);
this.mouse = new MouseStatus(canvas);
this.states.record.init(this);
this.states.save.init(this);
this.state = this.states.record;
this.state.enter();
this.state.execute();
};
I need the audEng, waveDisp and mouse instances to talk to each other - waveDisp needs to pull waveform data from audEng, click events received by waveDisp need to trigger state changes in the parent class and so on.
My first thought was to pass 'this' as a parameter into each instances constructor and store the reference as this.parent, then I could "reach back up" as necessary and directly call the query methods and state change methods I wanted. In this case I think I can justify directly reaching up into the parent class to call a state change method e.g this.parent.save() but... reaching back up into the parent then down again into another class e.g. this.parent.audEng.getWaveform() makes me feel uneasy, am I right in thinking this violates the law of demeter? If this is an unwise/smelly route to go down is the right solution making sure all the inter class communications I need are done via methods defined in parent RecorderApp class? Or is the architecture I'm suggesting fundamentally dumb.
Edit because marked as duplicate: The other answer I have seen on here is quite C++ centric, opinion seems divided, gives no explicit example and best answer didn't answer my question in a way that was completely clear to me, in particular "other languages use an object" is a bit more vague a solution than I was hoping for.