zoukankan      html  css  js  c++  java
  • 实现一个EventEmitter类,这个类包含以下方法: on/ once/fire/off

    实现一个EventEmitter类,这个类包含以下方法: on(监听事件,该事件可以被触发多次)- once(也是监听事件,但只能被触发一次)- fire(触发指定的事件)- off(移除指定事件的某个回调方法或者所有回调方法)
    function EventEmitter() {
        this.handlers = {}
    }
    //监听事件,该事件可以被触发多次
    EventEmitter.prototype.on = function (eventName, handle) {
        if (!this.handlers.hasOwnProperty(eventName)) {
            this.handlers[eventName] = []
        }
        this.handlers[eventName].push(handle)
    }
    //也是监听事件,但只能被触发一次
    EventEmitter.prototype.once = function (eventName, handle) {
        
    }
    //触发指定的事件
    EventEmitter.prototype.fire = function (eventName, ...params) {
        if (!this.handlers.hasOwnProperty(eventName)) return
        //事件队列依次执行
        this.handlers[eventName].map(handle => {
            handle(...params)
        })
    }
    //移除指定事件的某个回调方法或者所有回调方法
    EventEmitter.prototype.off = function (eventName, handle) {
        if (!this.handlers.hasOwnProperty(eventName)) return
        //获取下标,并删除
        let index = this.handlers[eventName].indexOf(handle)
        this.handlers[eventName].splice(index, 1)
    }
    
    const emitter = new EventEmitter();
    emitter.on('drink', (person) => {
    console.log(person + '喝水')
    })
    emitter.on('eat', (person) => {
    console.log(person + '吃东西')
    })
    // event.once('buy', (person) => {
    // console.log(person + '买东西')
    // })
    emitter.fire('drink', '我') // 我喝水
    emitter.fire('drink', '我') // 我喝水
    emitter.fire('eat', '其它人') // 其它人吃东西
    emitter.fire('eat', '其它人') // 其它人吃东西
    emitter.fire('buy', '其它人') //其它人买东西
    emitter.fire('buy', '其它人') //这里不会再次触发buy事件,因为once只能触发一次
    emitter.off('eat') //移除eat事件
    emitter.fire('eat', '其它人') //这里不会触发eat事件,因为已经移除了
    

      

  • 相关阅读:
    线段树总结
    c语言实现按层次(广度优先)非递归遍历二叉链树
    三、初学.NET—Gridview的分页
    五、初学.NET—Gridview自动编号和鼠标停留行加背景
    四、初学.NET—Gridview外部按钮选中、删除一行
    二、初学.NET—Gridview的排序
    一、初学.NET—数据库连接字符串
    .net web study
    C# 接口
    c#索引器
  • 原文地址:https://www.cnblogs.com/yz-blog/p/11312596.html
Copyright © 2011-2022 走看看