js实现一个事件类,包含on/off/emit/once方法
2020-07-14 本文已影响0人
Yong_bcf4
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);
}