实现EventBus的四种方法:on、once、emit、delete
// 构造EventBus
function EventBusClass() {
this.msgQueues = {}
}
EventBusClass.prototype = {
// 将消息保存到当前的消息队列中
on: function(msgName, func) {
if (this.msgQueues.hasOwnProperty(msgName)) {
if (typeof this.msgQueues[msgName] === 'function') {
this.msgQueues[msgName] = [this.msgQueues[msgName], func]
} else {
this.msgQueues[msgName] = [...this.msgQueues[msgName], func]
}
} else {
this.msgQueues[msgName] = func;
}
},
// 消息队列中仅保存一个消息
once: function(msgName, func) {
// 无需检查msgName是否存在
this.msgQueues[msgName] = func;
},
// 发送消息
emit: function(msgName, msg) {
if (!this.msgQueues.hasOwnProperty(msgName)) {
return
}
if (typeof this.msgQueues[msgName] === 'function') {
this.msgQueues[msgName](msg)
} else {
this.msgQueues[msgName].map((fn) => {
fn(msg)
})
}
},
// 移除消息
off: function(msgName) {
if (!this.msgQueues.hasOwnProperty(msgName)) {
return
}
delete this.msgQueues[msgName]
}
}
// 将EventBus放到window对象中
const EventBus = new EventBusClass()
window.EventBus = EventBus
function func(arg){
console.log(arg);
}
window.EventBus.on('msgName',func);
window.EventBus.emit('msgName',999);