eventEmitter

wade-mac:fin_server_invest mac$ node
> var events =require('events')
undefined
> var eventEmitter = new events.EventEmitter();
undefined
> var ringBell = function ringBell()
... {
...   console.log('ring ring ring');
... }
undefined
> eventEmitter.on('doorOpen', ringBell);
{ domain: null,
  _events: { doorOpen: [Function: ringBell] },
  _maxListeners: 10 }
> eventEmitter.emit('doorOpen')
ring ring ring
true
> var ringBB = function ringBB(name, age){
... console.log('bb', name, age);
... }
undefined
> eventEmitter.on('bbcry', ringBB);
{ domain: null,
  _events: 
   { doorOpen: [Function: ringBell],
     bbcry: [Function: ringBB] },
  _maxListeners: 10 }
> eventEmitter.emit('doorOpen');
ring ring ring
true
> eventEmitter.emit('bbcry', 'wade', 3);
bb wade 3
true
> var callDaddy = function callDaddy(name,age){
... console.log('call daddy', name, age);
... }
undefined
> eventEmitter.on('bbcry',callDaddy);
{ domain: null,
  _events: 
   { doorOpen: [Function: ringBell],
     bbcry: 
      [ [Function: ringBB],
        [Function: callDaddy] ] },
  _maxListeners: 10 }
> eventEmitter.emit('bbcry', 'wade', 3);
bb wade 3
call daddy wade 3
true
> 

 

你可能感兴趣的:(event)