A complete example of a simple subscriber and publisher pattern in JS mode

Source: Internet
Author: User

This article illustrates the simple subscriber and publisher pattern of JS mode. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/> <title>parten</title> </head> <body> <script> var singletontest = Singletontest.getinstance ({pointx:5}); Console.log (SINGLETONTEST.POINTX); Easy_observer_model; function Observerlist () {this.observerlist = [];}; OBSERVERLIST.PROTOTYPE.ADD = function (obj) {return this.observerList.push (obj); ObserverList.prototype.Empty = function () {this.observerlist = [];}; ObserverList.prototype.Count = function () {return this.observerList.length;}; ObserverList.prototype.Get = function (index) {if (index>-1 && index<this.observerlist.length) return This.observerlist[index]; }; ObserverList.prototype.Insert = function (obj,index) {var pointer =-1; if (index = = 0) {this.observerList.unsHift (obj); pointer = index; }else if (index = = this.observerList.length) {this.observerList.push (obj); pointer = index;}; return pointer; }; ObserverList.prototype.IndexOf = function (obj,startindex) {var i = startIndex, pointer =-1; while (I < This.observerlis T.length) {if (this.observerlist[i] = = obj) {pointer = i;}; i++}; return pointer; }; ObserverList.prototype.RemoveIndexAt = function (index) {if (index = = 0) {this.observerList.shift ();} else if (index = = this.observerlist.length-1) {This.observerList.pop ();}; return index; }; function Extend (obj,extension) {for (var key in obj) {Extension[key] = Obj[key];}; function Subject () {this.observers = new observerlist ();}; Subject.prototype.AddObserver = function (obj) {this.observers.add (obj)}; Subject.prototype.RemoveObserver = function (Observer) {This.observers.removeIndexAt (This.observers.IndexOf ( observer,0)); }; Subject.prototype.Notify = function (context) {var observercount = This.observers.count (); for (var i=0; i<oBservercount; i++) {This.observers.Get (i). Update (context); }//pubsub//subscribe var Pubsub = {}; (function (q) {var topics = [], subuid =-1; q.publish = function (Topic,args) {if (!topics[topic]) {return false;; var Sub Scribers = Topics[topic], len = subscribers? subscribers.length:0; while (len--) {subscribers[len].func (Topic,args); }; Q.subscribe = function (Topic,func) {if (!topics[topic]) {topics[topic] = [];}, var token = (++subuid). toString (); topics[t Opic].push ({token:token, func:func}); return token; }; Q.unsubscribe = function (token) {for (var m-topics) {if (Topics[m]) {for (var i=0; i<topics[m].length; i++) {if (topics[ M][i].token = = token) {topics[m].splice (i,1); return token;}} }; }; return this; }) (PubSub); </script> </body> </html>

The

wants this article to help you with your JavaScript programming.

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.