php 发布订阅模式,javascript发布订阅模式的详解(附示例)

本篇文章给大家带来的内容是关于javascript发布订阅模式的详解(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

发布订阅模式

事件发布/订阅模式 (PubSub) 在异步编程中帮助我们完成更松的解耦,甚至在 MVC、MVVC 的架构中以及设计模式中也少不了发布-订阅模式的参与。

优点:在异步编程中实现更深的解耦

缺点:如果过多的使用发布订阅模式,会增加维护的难度

实现发布订阅模式var Event = function() {

this.obj = {}

}

Event.prototype.on = function(eventType,fn) {

if(!this.obj[eventType]) {

this.obj[eventType] = []

}

this.obj[eventType].push(fn)

}

Event.prototype.emit = function() {

// 取第一个参数,作为eventType

var eventType = Array.prototype.shift.call(arguments);

// 获取事件数组

var arr = this.obj[eventType];

var len = arr.length;

// 循环数组,一次执行其中的函数

for(var i=0;i

// 直接调用arr[i],其this指向为undefined(严格模式下)

// 因此用apply将this指向arr[i]

// 数组shift函数取出第一个参数,将剩下的参数传入函数中

arr[i].apply(arr[i],arguments)

}

}

var ev = new Event()

ev.on('click',function(a) { // 订阅

console.log(a)

})

ev.emit('click',1) // 发布

以上代码只能实现先订阅,再发布。直接发布就会报错。如何实现可以先发布,然后订阅?var Event = function() {

this.obj = {};

this.cacheList = [];

}

Event.prototype.emit = function() {

const args = arguments; //函数参数

const that = this; //this指向,保持cache函数的this指向

function cache() {

var eventType = Array.prototype.shift.call(arg)

var arr = that.obj[eventType]

for (let i = 0; i < arr.length; i++) {

arr[i].apply(arr[i], arg)

}

}

this.cacheList.push(cache) // 采用闭包,保持对emit函数中参数和that的引用

}

Event.prototype.on = function(eventType,fn) {

if(!this.obj[eventType]) {

this.obj[eventType] = []

}

this.obj[eventType].push(fn)

// 在订阅函数中执行emit函数中缓存的函数

for (let i = 0; i < this.cacheList.length; i++) {

this.cacheList[i]()

}

}

改成这样后就实现了先发布函数,再订阅的过程。但是也只能先发布,然后再订阅,反过来就行不通。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值