function Emitter() {
}
Emitter.prototype = function() {
  var events = {};
  return {
    constructor: Emitter,
    on: function(type, cb) {
      var arr = events[type] = events[type] || [];
      (arr.indexOf(cb) === -1) && arr.push(cb);
      
      return this;
    },
    off: function(type, cb) {
      var arr = events[type] = events[type] || [], i = 0;
      while(arr.length !== i) {
        if (arr[i] === cb) {
          arr.splice(i, 1);
          break;
        }
        i++;
      }
      return this;
    },
    emitter: function(type) {
      var arr = events[type] = events[type] || [], i = 0;
      while(arr.length !== i) {
        arr[i].apply(this, [].slice.call(arguments, 1));
        i++;
      }
      return this;
    }
  }
}();

erichooooow
229 声望5 粉丝

simple is not easy