如何使用这个基于函数的事件发射器?
How to use this function based Event Emitter?
在this old answer中是否有以下基于函数的事件发射器。
也许它的使用方法非常明显,但对我来说不是,所以如果有人可以用它制作一个 hello world 示例,我将不胜感激。
有人知道怎么用吗?
function EventBase() {
this.events = {};
};
EventBase.prototype = {
on: function (event, listener) {
if (typeof event !== 'string') throw new TypeError("Event must be a string");
if (typeof event !== 'string') throw new TypeError("Listener must be a function");
this.events[event] || (this.events[event] = []);
this.events[event].push(listener);
},
off: function (event, listener) {
if (arguments.length === 0) {
// remove all listeners
this.events = {};
return;
}
if (!this.events[event]) {
// return if there's no event by the given name
return;
}
if (arguments.length === 1) {
// remove all listeners for the given event
delete this.events[event];
return;
}
// remove specific listener
this.events[event] = this.events[event].filter(function (func) {
return func !== listener;
});
},
emit: function (event) {
if (!this.events[event]) {
// return if there's no event by the given name
return;
}
// get args
var args = [].slice.call(arguments, 1);
// invoke listeners
this.events[event].forEach(listener => listener.apply(this, args));
},
getListenerCount: function (event) {
// get total number of listeners
if (arguments.length === 0) {
return Object.keys(this.events).reduce((count, key) => count + this.getListenerCount(key), 0);
}
// return zero for non-existing events
if (!this.events[event]) {
return 0;
}
// return count for specific event
return this.events[event].length;
}
};
EventBase 是构造函数。我们实例化之后,就可以调用原型中定义的方法了。 on
方法允许您将侦听器回调添加到某个事件名称。 emit
允许发出一个事件名称,然后将触发侦听该事件的侦听器
const event = new EventBase()
event.on("connect", () => {
console.log("connected")
})
event.emit("connect")
在this old answer中是否有以下基于函数的事件发射器。
也许它的使用方法非常明显,但对我来说不是,所以如果有人可以用它制作一个 hello world 示例,我将不胜感激。
有人知道怎么用吗?
function EventBase() {
this.events = {};
};
EventBase.prototype = {
on: function (event, listener) {
if (typeof event !== 'string') throw new TypeError("Event must be a string");
if (typeof event !== 'string') throw new TypeError("Listener must be a function");
this.events[event] || (this.events[event] = []);
this.events[event].push(listener);
},
off: function (event, listener) {
if (arguments.length === 0) {
// remove all listeners
this.events = {};
return;
}
if (!this.events[event]) {
// return if there's no event by the given name
return;
}
if (arguments.length === 1) {
// remove all listeners for the given event
delete this.events[event];
return;
}
// remove specific listener
this.events[event] = this.events[event].filter(function (func) {
return func !== listener;
});
},
emit: function (event) {
if (!this.events[event]) {
// return if there's no event by the given name
return;
}
// get args
var args = [].slice.call(arguments, 1);
// invoke listeners
this.events[event].forEach(listener => listener.apply(this, args));
},
getListenerCount: function (event) {
// get total number of listeners
if (arguments.length === 0) {
return Object.keys(this.events).reduce((count, key) => count + this.getListenerCount(key), 0);
}
// return zero for non-existing events
if (!this.events[event]) {
return 0;
}
// return count for specific event
return this.events[event].length;
}
};
EventBase 是构造函数。我们实例化之后,就可以调用原型中定义的方法了。 on
方法允许您将侦听器回调添加到某个事件名称。 emit
允许发出一个事件名称,然后将触发侦听该事件的侦听器
const event = new EventBase()
event.on("connect", () => {
console.log("connected")
})
event.emit("connect")