Observables
Observable
Observer
Subject
subscriber – subscribe()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function Hunter(name, level) { this.name = name; this.level = level; this.task = []; }
Hunter.prototype.publish = function (money) { console.log(this.level + "猎人" + this.name + '寻求帮助'); this.list.forEach( (item, index) => item(money)) }
Hunter.prototype.subscribe = function (target, fn) { console.log(this.level + "猎人" + this.name + '订阅任务'); target.list.push(fn) }
let HunterZhang = new Hunter('小张', 'level1'); let HunterMing = new Hunter('小明', 'level5'); let HunterWang = new Hunter('小王', 'level2'); let HunterLi = new Hunter('小李', 'level3');
|