js观察这模式

js观察者模式

// config/Event.js

class Event {
    handlers;

    constructor() {
        this.handlers={}
    }

    on (type, fn) {
        this.handlers[type] = this.handlers[type] || [];
        this.handlers[type].unshift(fn);
        return this;
    }

    off(type, fn) {
        const arr = this.handlers[type];
        if(arr) {
            if(!fn) {
                delete this.handlers[type];
                return this;
            }
            const index =arr.indexOf(fn)
            arr.splice(index, 1)
        }
        return this;
    }

    trigger(type, ...rest) {
        if(!this.handlers[type]) {
            return;
        }
        const args = rest;
        for (let index = 0; index < this.handlers[type].length; index++) {
            const fn = this.handlers[type][index];
            fn.apply(fn, args);
        }
        return this;
    }




}

export const globalEvent = new Event();

 

posted @ 2020-09-25 09:50  happynood  阅读(89)  评论(0编辑  收藏  举报