获取订阅的事件的名称
函数 EventEmitter.eventNames()
将返回一个数组,其中包含当前订阅的事件的名称。
const EventEmitter = require("events");
class MyEmitter extends EventEmitter{}
var emitter = new MyEmitter();
emitter
.on("message", function(){ //listen for message event
console.log("a message was emitted!");
})
.on("message", function(){ //listen for message event
console.log("this is not the right message");
})
.on("data", function(){ //listen for data event
console.log("a data just occured!!");
});
console.log(emitter.eventNames()); //=> ["message","data"]
emitter.removeAllListeners("data");//=> removeAllListeners to data event
console.log(emitter.eventNames()); //=> ["message"]