zoukankan      html  css  js  c++  java
  • eventEmitter 简单实现

    class EventEmmiter {
        constructor() {
            this._events = {}
        }
        on(type, cb) {
            if(Array.isArray(type)) {
                type.forEach(tp => this.on(tp, cb))
            } else {
                (this._events[type] || (this._events[type] = [])).push(cb);
                return this;
            }
        }
        emit(type) {
            const cbs = this._events[type];
            const args = Array.prototype.slice.call(arguments, 1);
            cbs && cbs.forEach(cb => cb.apply(this, args));
            return this;
        }
        off(type, cb) {
            if(!arguments.length) {
                this._events = new Object(null);
                return this;
            }
            if(Array.isArray(type)) {
                type.forEach(tp => this.off(tp, cb));
                return this;
            }
            if(!cb) {
                this._events[type] === null;
                return this;
            }
            if(cb) {
                const cbs = this._events[type];
                for(let len = cbs.length, i = len -1; i >= 0; i--) {
                    if(cbs[i] === cb || cbs[i].fn === cb) {
                        cbs.splice(i, 1);
                    }
                }
            }
            return this
        }
        once(type, cb) {
            function on() {
                this.off(type, cb);
                cb.apply(this, arguments);
            }
            on.fn = cb;
            this.on(type, on);
            return this;
        }
    }
    
  • 相关阅读:
    canvas的基本用法
    h5新增属性
    jquery中遍历
    git版本控制器
    bootstrap-datetimepicker时间插件
    layer.load的使用
    ajax请求json中的数据
    h5中input的request属性提示文字字段
    layUI
    v-show v-if 的使用
  • 原文地址:https://www.cnblogs.com/Mcrown/p/14435144.html
Copyright © 2011-2022 走看看