獲取訂閱的事件的名稱
函式 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"]