By publishing and subscribing to events instead of tightly coupling interaction between components, they can work together and in isolation, without needing to know about one another's existence.
Subscribe
subscribe(event, callback)
Subscribe to event, calling the callback event every time the specified event is published.
Similar to the behaviour of addEventListener, attempting to subscribe a function to the same event more than once will have no effect.
Arguments
event
The name of the event or events to be subscribed to, as a string. If multiple events are specified, they must be separated by whitespace.
callback
A function to call when the specified event is published.
Publish
publish(event, ...args)
Publish the specified event, and pass any number of arguments to each function subscribed to that event.
If multiple events are subscribed to an event, they are called in the order in which they were subscribed.
Arguments
event
The name of the event or events to be published, as a string. If multiple events are specified, they must be separated by whitespace.
args
Any number of arguments to be used when calling functions that are subscribed to the published event.
Unsubscribe
unsubscribe(event, callback)
Unsubscribe the callback function from the specified event, so it is no longer called when the event is published.
If a function is not subscribed to an event, attempting to unsubscribe it will have no effect.
Arguments
event
The name of the event to be unsubscribed from, as a string.
callback
A function that was subscribed to the specified event.