可以链式调用


  • 0

    class EventEmitter {
    constructor() {
    this.events = {};
    }

    on(eventName, callback) {
    if (!this.events[eventName]) {
    this.events[eventName] = []
    }
    this.events[eventName].push(callback);
    return this;
    }

    emit(eventName, ...args) {
    if (this.events[eventName].length) {
    this.events[eventName].forEach(fn => {
    if (typeof fn === 'function') {
    fn(...args);
    }
    });
    }
    return this;
    }

    off(eventName, callback) {
    if (this.events[eventName].length) {
    const pos = this.events[eventName].findIndex(fn => fn === callback);
    this.events[eventName].splice(pos, 1);
    } else {
    delete this.events[eventName];
    }
    return this;
    }
    }

    可以链式调用


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待