EventEmitter的实现 发表于 2018-06-11 | 分类于 设计模式 ¶ES6的实现 12345678910111213141516171819202122232425262728293031323334export class EventEmitter { constructor () { this.listenerList = {} } on (e, cb) { const cbs = this.listenerList[e] || [] if (typeof cb !== 'function') { throw new TypeError(`${cb} is not a function`) } else { cbs.push(cb) } this.listenerList[e] = cbs } emit (e) { const cbs = this.listenerList[e] const restArgs = Array.prototype.slice.call(arguments, 1) if (cbs && cbs.length) { cbs.forEach((cb) => { cb(...restArgs) }) } } off (e, fn) { const cbs = this.listenerList[e] if (cbs && cbs.length) { if (fn && cbs.includes(fn)) { const index = cbs.indexOf(fn) cbs.splice(index, 1) } else if (!fn) { delete this.listenerList[e] } } }} 点击打赏 微信支付 支付宝