题解 | #发布订阅模式#

发布订阅模式

http://www.nowcoder.com/practice/569e1fd641be4fd797f12a28b763d709

class EventEmitter {
    // 补全代码
    constructor(){
      this.obj={}
    }
    on(name,fn){
      let result = this.obj;
      if(!result[name]){
        result[name] = [fn]
      }else{
        result[name].push(fn)
      }
    }//不能用逗号
    emit(name){
      let result = this.obj;
      if(result[name]){
        for(let fun of result[name]){
          fun([].slice.call(arguments,1));
        }
      }
    }
}
全部评论

相关推荐

10-01 09:50
门头沟学院 Java
肖先生~:这个人真的很好,点赞
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务