- Widely used in asynchronous programming, it is an alternative to callback function. Subscribe to an event. The event occurs after operation A (listening to operation A)
- Instead of the hard coded notification mechanism between objects, one object does not need to call another object's interface to make the two objects loosely coupled. Although the details of each other are not clear, they do not affect the communication between them.
1. Dom event
A typical publishing subscriber mode. An event ('click, mousedown, etc ') listens to a dom node, operates the dom node, triggers the event, and executes the response function. The event function is completely unknown to the dom node. It's good to publish without paying attention to the content of the event function.
2. User defined events
- Specify publisher
- Give the publisher a cache list. An object consists of n key value pairs. The key represents the event name. The value is an array of event handlers, which is equivalent to the subscriber roster
- Publish the message, traverse the cache list, and execute the callback function of the subscriber in turn.
Simple publish and subscribe model and variable parameters