Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the functions of the subscription model published by js

Shulou Source: shulou.com Published: 2022-06-03 07:35:21 10月01日 Update

This article will explain in detail the role of the subscription model published by js, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Publish-subscribe mode can be widely used in asynchronous programming, which is a scheme instead of callback function.

2. Publish-subscribe mode can replace the hard-coded notification mechanism between objects, and one object no longer needs to explicitly call the interface of another object.

Example

/ / since these members are common to any publisher object, it makes sense to implement them as part of a separate object. That way we can copy it to any object and turn any given object into a publisher. / / implement a general publisher and define the publisher object as follows.... Let publisher = {subscribers: {any: []}, subscribe: function (fn, type = `any`) {if (typeof this.subscribers [type] = = `undefined`) {this.subscribers [type] = [];} this.subscribers [type] .push (fn);}, unSubscribe: function (fn, type = `any`) {let newSubscribers = [] (item, I) = > {if (item! = = fn) {newSubscribers.push (fn);}}); this.subscribers [type] = newSubscribers;}, publish: function (args, type = `any`) {this.subscribers [type] .forEach ((item, I) = > {item (args);});}} / define a function makePublisher () that takes an object as a parameter and converts it into a publisher function makePublisher (obj) {for (let i in publisher) {if (publisher.hasOwnProperty (I) & & typeof publisher [I] = `function`) {obj [I] = publisher [I];}} obj.subscribers = {any: []} by copying the above general publisher's method into the object } / / implement the paper object var paper = {daily: function () {this.publish (`monthly`);}, monthly: function () {this.publish (`interesting analysis`, `monthly`);}}; / / construct the paper into a publisher makePublisher (paper); / / look at the subscription object joe, which has two methods: var joe = {drinkCoffee: function (paper) {console.log (`Just read` + paper) }, sundayPreNap: function (monthly) {console.log (`About to fall asleep reading this `+ monthly);}; / / paper register joe (i.e. joe subscribes to paper) paper.subscribe (joe.drinkCoffee); paper.subscribe (joe.sundayPreNap, `monthly`); / / that joe provides a callable method for the default "any" event, while another callable method is used when an event of type "monthly" occurs. Now let's trigger some events: paper.daily (); / Just read big news todaypaper.daily (); / / Just read big news todaypaper.monthly (); / / About to fall asleep reading this interesting analysispaper.monthly (); / / About to fall asleep reading this interesting analysispaper.monthly () / / the role of About to fall asleep reading this interesting analysis's subscription model for js publishing has been shared here. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Objects publishers subscriptions patterns methods events actions contents functions articles more knowledge articles yes between two parameters instances cases members Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Xiaomi MariaDB MySQL