zoukankan      html  css  js  c++  java
  • 利用JS实现事件订阅的函数方法

    /*
    on 绑定

    emit 触发

    off 解绑

    //存放事件
    eventList = {
      key:val
      handle:[]
    }

     

    1对多
    on(eventName,callback);
    handle:-------N多个
    1、判断事件名称是否存在

    2、如果存在的情况下将cb存放在eventName这个数组当中

    3、如果不存在创建key值为eventName val值为数组

     

    1对多
    emit(eventName,params);

    当调用emit的时候获取到eventName这的值,对值判断,如果值不存在直接return
    如果存在遍历这个值全局进行调用,然后将params传入这些函数

     


    off(eventName,[callback])
    当调用off的时候获取到eventName这的值,对值判断,如果值不存在直接return
    如果存在判断callback是否存在 如果存在删除指定的函数
    如果不存在将当前数组清空


    */

    //代码如下:

    const EventList = {

    }


    const on = function(eventName,callback){
      if(!EventList[eventName]){
        EventList[eventName] = [];
      }

      EventList[eventName].push(callback);
    }


    const emit = function(eventName,params){
      if(!EventList[eventName])return;

      EventList[eventName].map((cb)=>{
        cb(params)
      })
    }


    const off = function(eventName,callback){
      if(!EventList[eventName])return;

      if(callback){
        let index = EventList[eventName].indexOf(callback);
        EventList[eventName].splice(index,1);
      }else{
        EventList[eventName] = [];
      }
    }

    export default {
      $on:on,
      $emit:emit,
      $off:off
    }

  • 相关阅读:
    【PAT】B1041 考试座位号(15 分)
    【PAT】B1042 字符统计(20 分)
    【PAT】B1044 火星数字(20 分)
    LeetCode 3Sum Closest
    一定要做的事(备忘)
    LeetCode Integer to Roman
    Container With Most Water
    LeetCode ZigZag Conversion
    LeetCode 5 最长回文子串 Manacher线性算法
    LeetCode Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/dongyuezhuang/p/11472602.html
Copyright © 2011-2022 走看看