let HunterUnion = {
type: 'DOM',
topics: Object.create(null),
subscribe: function (topic, fn){
if(!this.topics[topic]){
this.topics[topic] = [];
}
this.topics[topic].push(fn);
},
publish: function (topic, money){
if(!this.topics[topic])
return;
for(let fn of this.topics[topic]){
fn(money)
}
}
}
function DOM (name, level) {
this.name = name
this.level = level
this.list = []
}
DOM.prototype.addListener = function (topic, fn){
console.log(this.level + '猎人' + this.name + '订阅了狩猎' + topic + '的任务')
HunterUnion.subscribe(topic, fn)
}
DOM.prototype.publish = function (topic, money){
console.log(this.level + '猎人' + this.name + '发布了狩猎' + topic + '的任务')
HunterUnion.publish(topic, money)
}
let hunterMing = new DOM('小明', '黄金')
let hunterJin = new DOM('小金', '白银')
let hunterZhang = new DOM('小张', '黄金')
let hunterPeter = new DOM('Peter', '青铜')
hunterMing.addListener('tiger', money => {
console.log('小明表示:' + (money > 200 ? '' : '暂时很忙,不能') + '给予帮助')
} )
hunterJin.addListener('tiger', function(){
console.log('小金表示:给予帮助')
})
hunterZhang.addListener('tiger', function(){
console.log('小张表示:给予帮助')
})
hunterPeter.publish('tiger',198)
console