考察行为型模式与模块解耦
概念
简易实现
class EventBus {
constructor() { this.map = {}; }
on(t, fn) { (this.map[t] ||= []).push(fn); }
emit(t, data) { (this.map[t] || []).forEach(fn => fn(data)); }
}
前端状态管理(Redux/Vuex)、DOM 事件均基于此思想:组件只关心事件名,不关心谁触发,耦合度更低。
题目与答案就在本页;收藏、批注和错题重练在应用内