class EventHub{
map={}
on(name,fn){
this.map[name] = this.map[name] || []
this.map[name].push(fn)
}
emit(name,data){
const fnList = this.map[name]
fnList.forEach(fn => fn.call(undefined,data));
}
off(name,fn){
const fnList = this.map[name]
const index = fnList.indexOf(fn)
if(index<0){return}
fnList.splice(index,1)
}
}
const eventHub = new EventHubs();
function greetListener(data) {
console.log(`Hello, ${data}!`);
}
function countListener(data) {
console.log(`Count: ${data}`);
}
eventHub.on('greet', greetListener);
eventHub.on('count', countListener);
eventHub.emit('greet', 'John');
eventHub.emit('count', 10);
eventHub.off('greet', greetListener);
eventHub.emit('greet', 'Alice');