var EventEmitter = require('events').EventEmitter;var emitter = new EventEmitter();console.log(emitter.getMaxListeners());//設置可監聽事件的最大個數emitter.setMax...
var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); console.log(emitter.getMaxListeners()); //設置可監聽事件的最大個數 emitter.setMaxListeners(11); function work(who){ console.log(who + ' go to work') } //監聽事件 emitter.on('do', work); emitter.on('do', function(who) { console.log(who + ' wash clothers') }); emitter.on('rest', function(who) { console.log(who + ' can have a rest'); }) //移除監聽事件 emitter.removeListener('do', work); //觸發事件 emitter.emit('do', 'John'); emitter.emit('do', 'lily'); emitter.emit('rest', 'Tom'); //所有‘do’的監聽事件 console.log(emitter.listeners('do')) //監聽事件的個數 console.log(emitter.listenerCount('do'));