<script>
var shoeObj = {
list: {},
listen: function (key, fn) {
// debugger
if (!this.list[key]) {
this.list[key] = []
}
shoeObj.list[key].push(fn)
},
trigger: function (key, ...b) {
console.log(shoeObj);
for (let index = 0; index < shoeObj.list[key].length; index++) {
let fn = shoeObj.list[key][index]
fn.apply(this, b)
}
}
}
shoeObj.listen('tao', () => {
console.log(1);
})
shoeObj.listen('tao',() => {
console.log(2);
})
shoeObj.trigger('tao')
</script>
要点
1.建立一个对象存包含不同订阅者的数组
2.发布的时找到对应订阅人的数组,循坏 这个数组进行调用