zoukankan      html  css  js  c++  java
  • 观察者模式

    class PubSub {
      constructor() {
        this.handles = {}
      }
    
      // 发布事件
      on(eventType, handle) {
        if(!this.handles.hasOwnProperty(eventType)) {
          this.handles[eventType] = []
        }
        if(typeof handle === 'function') {
          this.handles[eventType].push(handle)
        } else {
          throw new Error('缺少回调函数')
        }
        return this
      }
      // 订阅事件
      emit(eventType, ...args) {
        if(this.handles.hasOwnProperty(eventType)) {
          this.handles[eventType].forEach((item, key, arr) => {
            item.apply(null, args)
          })
        } else {
          throw new Error(`${eventType}`)
        }
        return this
      }
    
      // 删除事件
      off(eventType, handle) {
        if(!this.handles.hasOwnProperty(eventType)) {
          throw new Error(`${eventType}事件未注册`)
        } else if(typeof handle != 'function') {
          throw new Error('缺少回调函数')
        } else {
          this.handles[eventType].forEach((item, key, arr) => {
            if(item == handle) {
              arr.splice(key, 1)
            }
          })
        }
        return this
      }
    }
    let callback = function () {
      console.log('you are so nice');
    }
    let pubsub = new PubSub();
    pubsub.on('completed', (...args) => {
      console.log(args.join(' '));
    }).on('completed', callback);
    pubsub.emit('completed', 'what', 'a', 'fucking day');
  • 相关阅读:
    136-如何访问redis数据库
    135-如何实现result风格
    134-SpringMVC中的值,会有一个默认值
    133-this知识点
    132-SpringBoot中的请求方法
    034-405是什么错误?
    131-逆向工程配置文件
    SQL---实验一
    《将博客搬至CSDN》
    正则表达式1---QQ号合法性判断
  • 原文地址:https://www.cnblogs.com/Roxxane/p/14760222.html
Copyright © 2011-2022 走看看