// 简单的订阅/发布模式 class Event{ eventArr= [] constructor() { } on(name, fn) { if (!this.eventArr[name]) { this.eventArr.push(fn); } } emit() { console.log('发布啦....'); this.eventArr.forEach(function(fn){ fn(); }) } } const obj= new Event(); obj.on('fn1', function(){ console.log('fn1....') }) obj.on('fn2', function(){ console.log('fn2....') }) obj.on('fn3', function(){ console.log('fn3....') }) obj.emit();