Referencing the Eventemitter in the events module
Monitoring and launching of events
The same number of event launches is limited, and peaks can be set via Setmaxlisteners
var Eventemitter = require (' Events '). Eventemittervarnew eventemitter ()// plus no warning // Life.setmaxlisteners(life.on) (' clickevent ',function(WHO) { Console.log ( ' give ' + who + ' pour water ')}) Life.emit (' clickevent ', ' Man 1 ')
Each emit has a return value indicating whether there is a corresponding listener event
var hasclicklistener = life.emit (' clickevent ', ' Man ')var haschangelistener = Life.emit (' ChangeEvent ', ' sister 1 ') console.log (Hasclicklistener) console.log (Haschangelistener) console.log (Hasputlistener)
Cancel Monitoring
//can be placed behind on and emit .functionWater (WHO) {Console.log (' Give ' + who + ' pour water ')}life.on (' ChangeEvent ', water)//Has not been canceled, emit cannot be put on beforeLife.emit (' ChangeEvent ', ' Zhang San ')//water can not use anonymous functions, RemoveListener need to be before emitLife.removelistener (' ChangeEvent ', water)//Remove all listeners, you can pass event names as parameters//life.removealllisteners ()varHaschangelistener = Life.emit (' changeevent ', ' sister 1 ')//The listener was canceled .varHasputlistener = Life.emit (' clickevent ', ' Li Bai ')////Whether it is monitoredConsole.log (Haschangelistener) console.log (Hasputlistener)
Number of listeners
Console.log (' changeevent: ' + life.listeners (' ChangeEvent '). Length) Console.log (' clickevent: ' + Life.listeners (' clickevent '). Length) Console.log (' all: ' + life.listeners.length)// Eventemitter provides the method Console.log (' Eventemitter provides: ') console.log (' changeevent: ' + Eventemitter.listenercount (Life, ' ChangeEvent ')) Console.log (' clickevent: ' + eventemitter.listenercount ( Life, ' clickevent '))
08 Mu Class Network "Attack on node. JS Foundation (i)" Events