zoukankan      html  css  js  c++  java
  • wather.js 封装

    /** @format */
    /* eslint-disable @typescript-eslint/no-explicit-any */
    /* eslint-disable @typescript-eslint/explicit-module-boundary-types */

    // 观察者
    class Watcher {
        public list: any;
        constructor() {
          this.list = {};
        }
        // 订阅
        public $on(key: string, fn: any): void {
          if (!this.list[key]) {
            this.list[key] = [];
          }
          this.list[key].push(fn);
        }
        // 发布
        public $emit(key: string, param?: any): void | boolean {
          const fns = this.list[key];
          // 检测是否有注册  注册后是否有注入方法
          if (!fns || fns.length === 0) {
            return false;
          }
          // 依次执行 并传入参数
          fns.forEach((fn: any) => {
            fn(param);
          });
        }
        // 清空
        public $clear(key: string) {
          this.list[key] = [];
        }
        public $clearAll() {
          this.list = {};
        }
      }
      
      export default new Watcher();  
  • 相关阅读:
    单例模式
    iOS宏定义
    WKWebView基本使用
    文件操作(NSFileManager)
    iOS 字典和NSData之间转换
    iOS 身份证,邮箱,手机号验证
    iOS自定义数字键盘
    iOS指纹识别
    JavaScript表单
    JavaScript数组操作方法集合(2)
  • 原文地址:https://www.cnblogs.com/wenqylh/p/14964946.html
Copyright © 2011-2022 走看看