编写一个自定义事件类,包含on/off/emit/once方法

function Event() {
  this._events = {};
}

Event.prototype.on = function(type, fn) {
  if (!this._events[type]) {
    this._events[type] = []
  }
  this._events[type].push(fn);
}

Event.prototype.off = function(type, fn) {
  if (!this._events[type]) {
    return;
  }
  if (!fn) {
    this._events[type] = undefined;
    return;
  }
  var index = this._events[type].indexOf(fn);
  this._events[type].splice(index, 1);
}

Event.prototype.emit = function(type) {
  if (!this._events[type]) {
    return;
  }
  this._events[type].forEach(fn => fn());
}

Event.prototype.once = function(type, fn) {
  var _ = this;
  var _fn = () => {
    fn.apply(_, arguments);
    this.off(type);
  };

  this.on(type, _fn);
}

猜你喜欢

转载自www.cnblogs.com/fazero/p/11450730.html