This article mainly introduces the principles and implementation methods of the JavaScript observer mode (publishsubscribe), briefly analyzes the principles and functions of the javascript observer mode, and provides the implementation skills of the observer mode in the form of examples, for more information about the principles and implementation methods of the JavaScript observer mode (publish/subscribe, this article briefly analyzes the principles and functions of the javascript observer mode and provides the implementation skills of the observer mode based on the instance form. For more information, see
This article describes the principles and implementation methods of the Javascript observer mode (publish/subscribe. We will share this with you for your reference. The details are as follows:
The observer mode is also called the publish and subscribe mode. It defines a one-to-many relationship, allowing multiple observer objects to listen to a topic object at the same time, when the status of this topic object changes, it notifies all the observed objects. It is composed of two types of objects, the topic and the observer, the topic is responsible for releasing events, and the observer observes the subject by subscribing to these events. The publisher and the subscriber are completely decoupled, they do not know each other's existence. They only share the name of a custom event.
In Nodejs, EventEmitter is used to implement native support for this mode. The event listening mechanism in Javascript can be understood as an observer mode.
The following is a javascript custom PubSub. Read the following code to help you understand the observer mode. For more information about the code, see github.
Function PubSub () {this. handlers ={};} PubSub. prototype = {// subscribe to events on: function (eventType, handler) {var self = this; if (! (EventType in self. handlers) {self. handlers [eventType] = [];} self. handlers [eventType]. push (handler); return this;}, // trigger event (publish event) emit: function (eventType) {var self = this; var handlerArgs = Array. prototype. slice. call (arguments, 1); for (var I = 0; I <self. handlers [eventType]. length; I ++) {self. handlers [eventType] [I]. apply (self, handlerArgs);} return self;}, // Delete subscription event off: function (eventType, handler) {var currentEvent = this. handlers [eventType]; var len = 0; if (currentEvent) {len = currentEvent. length; for (var I = len-1; I> = 0; I --) {if (currentEvent [I] === handler) {currentEvent. splice (I, 1) ;}}} return this ;}}; var pubsub = new PubSub (); var callback = function (data) {console. log (data) ;}; // subscribe to the event Apubsub. on ('A', function (data) {console. log (1 + data) ;}); pubsub. on ('A', function (data) {console. log (2 + data) ;}); pubsub. on ('A', callback); // triggers the event Apubsub. emit ('A', 'I am A parameter'); // deletes the event A's subscription source callbackpubsub. off ('A', callback); pubsub. emit ('A', 'I am the parameter of the second call ');
Running result.
The above is a detailed analysis of the principles and functions of the javascript observer mode. For more information, see other related articles in the first PHP community!