Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of the subscription model published by js

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report