BroadcastChannel API is the solution for inter-instance communication.
postMessage(message): Sends the given message to other BroadcastChannel objects set up for this channel.
close: Closes the BroadcastChannel object, opening it up to garbage collection.
onmessage: Event handler, the event will be triggered when the instance receive a message.
Run the code above, Stack can receive the message from Hulk.
One thing should be noticed: The
message object is not deep cloned.
See the demo below:
In this case,
event.data is strict equal to
Compared to the deep clone, this behavior improves efficiency and reduces memory cost. However, developers are not recommended to cache or modify the
event object when they are using it (All type of event handlers should have this restriction).