1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
/** * Created by ryan on 2016/8/29. */ 'use strict'; class Event { constructor() { this.subscribers = new Map([['any', []]]); }
on(fn, type = 'any') { let subs = this.subscribers; if (!subs.get(type)) return subs.set(type, [fn]); subs.set(type, (subs.get(type).push(fn))); }
emit(content, type = 'any') { for (let fn of this.subscribers.get(type)) { fn(content); } } }
let event = new Event();
event.on((content) => console.log(`get published content: ${content}`), 'myEvent'); event.emit('jaja', 'myEvent'); //get published content: jaja
|
近期评论