eventBus的实现
作者:
小杰66 | 来源:发表于
2021-04-04 07:08 被阅读0次//即发布订阅模式的简单实现
class EventEmeitter {
constructor() {
this.events = {};
}
emit(type, ...args) {
(this.events[type] || []).forEach((fun) => fun.apply(this, args));
}
addListener(type, fun) {
if (!this.events[type]) this.events[type] = [];
this.events[type].push(fun);
}
removeListener(type, fun) {
let funs = this.events[type] || [];
let index = funs.indexOf(fun);
if (index > -1) {
this.events[type].splice(index, 1);
}
}
}
本文标题:eventBus的实现
本文链接:https://www.haomeiwen.com/subject/jtjahltx.html
网友评论