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();  
  • 相关阅读:
    线性表的相关操作
    jq实现登陆页面的拖拽功能
    js实现登陆页面的拖拽功能
    HTML5存储
    js函数声明
    js打字机效果实现
    js日期显示效果
    js实现倒计时效果
    js中如何去获取外部css样式
    面向对象的几种方法详解(后)
  • 原文地址:https://www.cnblogs.com/wenqylh/p/14964946.html
Copyright © 2011-2022 走看看