https://scriptoj.com/problems/36
class EventEmitter {
constructor() {
this.messageBox = {};
}
on(eventName, func) {
const callbacks = this.messageBox[eventName] || [];
callbacks.push(func);
this.messageBox[eventName] = callbacks;
}
emit(eventName, ...args) {
const callbacks = this.messageBox[eventName];
if (callbacks.length > 0) {
callbacks.forEach((callback) => {
callback(...args);
});
}
}
off(eventName, func) {
const callbacks = this.messageBox[eventName];
const index = callbacks.indexOf(func);
if (index !== -1) {
callbacks.splice(index, 1);
}
}
}
作者:waka
链接:https://www.jianshu.com/p/5cdd6822596c
來源:简书