I'm struggling to grasp the concept of object-oriented JavaScript, particularly in terms of how classes can communicate with each other. Let's consider an example using Babel:
We have a "ColorPalette" class that contains a list of colors
We also have a "Canvas" class that allows you to display a color from the list in ColorPalette
In my main script, I create instances of both classes like this:
const colorPalette = new ColorPalette();
const canvas = new Canvas();
And then I am able to execute commands such as:
canvas.draw(colorPalette.getActiveColor());
So far, so good. I understand how to use getters and setters, and I know how to pass data to methods. Excellent.
However, there are situations where I don't want the main script to handle all the details. For instance, the ColorPalette should always be aware of the most frequently used color on the Canvas. But within the ColorPalette class, I shouldn't need to know or care about the specific instance name of the Canvas class.
In my main script, I could do the following:
colorPalette.setMainColor(canvas.getMainColor());
But I run into issues when trying to achieve the same thing from within the ColorPalette class itself. The class shouldn't rely on knowing the instance name of the Canvas class ("canvas"). I believe my confusion lies in flawed logic here. Any guidance would be greatly appreciated. Thank you for your assistance.