Analyze the principles and functions of the javascript observer Mode

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.