PART 01
PART 02
// 遍历对象,给每个属性装上监听器function observe(target) {if (target && typeof target === 'object') {Object.keys(target).forEach(key => {defineReactive(target, key, target[key])})}}// 核心:Object.defineProperty 拦截 get/setfunction defineReactive(target, key, val) {observe(val) // 递归处理嵌套对象const dep = new Dep() // 每个属性一个依赖管理器Object.defineProperty(target, key, {enumerable: true,configurable: false,get() {// 读取时,收集依赖if (Dep.target) {dep.addSub(Dep.target)}return val},set(value) {if (val === value) returnval = value// 设置时,通知所有依赖更新dep.notify()}})}
class Dep {constructor() {this.subs = []}addSub(watcher) {this.subs.push(watcher)}notify() {this.subs.forEach(watcher => watcher.update())}}Dep.target = null
class Watcher {constructor(vm, key, callback) {this.vm = vmthis.key = keythis.callback = callback// 触发 get,收集依赖Dep.target = thisvm[key] // 读取属性Dep.target = null}update() {this.callback(this.vm[this.key])}}
PART 03
// 股票数据源class StockService {constructor() {this.stocks = {'AAPL': { price: 178.5, name: '苹果' },'GOOGL': { price: 142.3, name: '谷歌' },'TSLA': { price: 245.8, name: '特斯拉' }}this.subscribers = new Map() // 订阅者列表}// 订阅股票变化subscribe(stockCode, callback) {if (!this.subscribers.has(stockCode)) {this.subscribers.set(stockCode, [])}this.subscribers.get(stockCode).push(callback)}// 取消订阅unsubscribe(stockCode, callback) {const subs = this.subscribers.get(stockCode)if (subs) {const index = subs.indexOf(callback)if (index > -1) subs.splice(index, 1)}}// 更新价格,通知所有订阅者updatePrice(stockCode, newPrice) {if (this.stocks[stockCode]) {const oldPrice = this.stocks[stockCode].pricethis.stocks[stockCode].price = newPrice// 通知订阅者const subs = this.subscribers.get(stockCode)if (subs) {subs.forEach(callback => {callback({code: stockCode,name: this.stocks[stockCode].name,oldPrice,newPrice,change: ((newPrice - oldPrice) / oldPrice * 100).toFixed(2)})})}}}}
// 大盘看板const mainBoard = (data) => {console.log(`📊 大盘看板:${data.name} 最新价 $${data.newPrice} (${data.change}%)`)}// 涨跌幅看板const changeBoard = (data) => {const emoji = data.change >= 0 ? '📈' : '📉'console.log(`${emoji} 涨跌幅:${data.name}${data.change}%`)}// 成交提醒看板const alertBoard = (data) => {if (Math.abs(data.change) > 5) {console.log(`🚨 大幅波动提醒:${data.name} 涨跌 ${data.change}%!`)}}
const stockService = new StockService()// 订阅 AAPLstockService.subscribe('AAPL', mainBoard)stockService.subscribe('AAPL', changeBoard)stockService.subscribe('AAPL', alertBoard)// 价格变动,自动通知所有看板stockService.updatePrice('AAPL', 182.3)// 输出:// 📊 大盘看板:苹果 最新价 $182.3 (2.13%)// 📈 涨跌幅:苹果 2.13%
PART 04
class EventEmitter {constructor() {this.handlers = {} // 事件名 -> 回调队列}// 订阅事件on(eventName, callback) {if (!this.handlers[eventName]) {this.handlers[eventName] = []}this.handlers[eventName].push(callback)}// 发布事件emit(eventName, ...args) {const callbacks = this.handlers[eventName]if (callbacks) {callbacks.slice().forEach(cb => cb(...args))}}// 取消订阅off(eventName, callback) {const callbacks = this.handlers[eventName]if (callbacks) {const index = callbacks.indexOf(callback)if (index > -1) callbacks.splice(index, 1)}}// 只触发一次once(eventName, callback) {const wrapper = (...args) => {callback(...args)this.off(eventName, wrapper)}this.on(eventName, wrapper)}}
const emitter = new EventEmitter()// 监听用户登录emitter.on('login', (user) => {console.log(`${user.name} 登录了`)})// 监听消息emitter.on('message', (msg) => {console.log(`收到消息:${msg}`)})// 触发emitter.emit('login', { name: '张三' })emitter.emit('message', '你好!')
PART 05
发布者 → 直接 → 订阅者 发布者 → 事件总线 → 订阅者 class Publisher {constructor() {this.observers = []}add(observer) {this.observers.push(observer) // 直接持有引用}notify(data) {this.observers.forEach(o => o.update(data)) // 直接调用}}
发布-订阅模式 : 通过中间层解耦:class PubSub {constructor() {this.subscribers = {}}subscribe(topic, callback) {// 不需要知道谁在订阅if (!this.subscribers[topic]) {this.subscribers[topic] = []}this.subscribers[topic].push(callback)}publish(topic, data) {// 发布者也不需要知道谁订阅了const callbacks = this.subscribers[topic]if (callbacks) {callbacks.forEach(cb => cb(data))}}}
PART 06


