var observer={ list:[], subscribe:function(key,fn){ if(!this.list[key]){ this.list[key]=[] } this.list[key].push(fn) }, publish:function(){ const key=arguments[0] this.list[key].forEach(fn => { fn.apply(this,arguments); }); } } observer.subscribe('huawei',function(brand,model){ console.log(brand,model) }) observer.subscribe('xiaomi',function(brand,model){ console.log(brand,model) }) observer.publish('huawei','p5011') observer.publish('xiaomi','mi14') observer.publish('xiaomi','mi13')
标签:function,observer,brand,list,publish,key,设计模式 From: https://www.cnblogs.com/howhy/p/17861341.html