美文网首页
发布订阅模式

发布订阅模式

作者: 还是流星拳比较好 | 来源:发表于2018-10-24 12:04 被阅读0次

事件中心:eventhub

window.eventHub={
    events:{},
    emit(eventName,data){
        for(let key in this.events){
            if (key===eventName){
                let fnList=this.events[key]
                fnList.map((fn)=>{
                    fn.call(undefined,data)
                })
            }
        }
    },
    on(eventName,fn){
        if (this.events[eventName]===undefined){
            this.events[eventName]=[]
        }
        this.events[eventName].push(fn)
    }
}

相关文章

网友评论

      本文标题:发布订阅模式

      本文链接:https://www.haomeiwen.com/subject/wpbttqtx.html