SOURCE

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

  //on 订阅
  on (eventName, handler) {
  // 根据eventName,事件池有对应的事件数组,
   if(this._event[eventName]) {
      this._event[eventName].push(handler);
    } else {
      this._event[eventName] = [handler];
    }
  }
  emit (eventName) {
  // 根据eventName找到对应数组
    var events = this._event[eventName];

  //  取一下传进来的参数,方便给执行的函数. Bus.$emit(‘fulfilled’, ‘已成功’)
    var otherArgs = Array.prototype.slice.call(arguments,1);
    var that = this;
   
    events.forEach((event) => {
      event.apply(that, otherArgs);
    })
  }
  // 解除订阅
  off (eventName, handler) {
    var events = this._event[eventName];
// 设置事件不为handler的
    this._event[eventName] = events.filter((event) => {
      return event !== handler;
    })
  }
  // 订阅一次on(eventName,func),在func里触发真实handler,并解除订阅that
	// .off(eventName, func)
  once (eventName, handler) {
var that = this;
    function func () {
      var args = Array.prototype.slice.call(arguments,0);
      handler.apply(that, args);
      that.off(eventName,func);
    }
    this.on(eventName, func);   // !!!是on,而不是emit!!!
  }
}


class EventEmitter {
    constructor() {
        this._event = {}
    }

    on(eventName, handler) {
        if (this._event[eventName]) {
            this._event[eventName].push(handler)
        } else{
            this._event[eventName] = [handler]
        }
    }

    emit(eventName) {
        const events = this._event[eventName];
        const restParam = Array.prototype.slice.call(arguments, 1);
        const that = this;

        events.forEach(event => {
            event.apply(that, restParam)
        })
    }

    off(eventName, handler) {
        const events = this._event[eventName];

        this._event = events.filter(event => {
            return event !== handler;
        })
    }

    once(eventName, handler) {
        const that = this;

        function func() {
            const rest = Array.prototype.slice.call(arguments, 0);

            handler.apply(that, rest);

            this.off(eventName, func)
        }

        this.on(eventName, func)
    }
}


// BinarySearch
function BinarySearch(key, array) {
    let start = 0;
    let end = array.length - 1;
    let m = ~~((start + end) / 2);

    while(start < end && key !== array[m]) {
        if (key > array[m]) {
            start = m + 1;
        } else {
            end = m - 1;
        }

        m = ~~((start + end) / 2);
    }
}
console 命令行工具 X clear

                    
>
console