首先测试用例:
var EventEmitter = require(‘events‘).EventEmittervar life = new EventEmitter();//life.on(‘求安慰‘, function(who){ ?console.log(‘xxxx‘)})function check(){ ?console.log(‘xxxx4354‘)}life.on(‘boys‘,check)// 移除监听life.removeListener(‘boys‘, check)var hasconforListeren = life.emit(‘求安慰‘, ‘汉子‘);var hasconforListeren = life.emit(‘boys‘, check);console.log(hasconforListeren)console.log(life.listeners(‘boys‘))console.log(EventEmitter.listenerCount(life, ‘求安慰‘))
总结:
/*
.on(‘事件名‘,function(){}) ---相当于 bind,但是不会触发
.emit(‘事件名‘)--- 相当于触发事件
*/
nodejs 中on 和 emit
原文地址:http://www.cnblogs.com/yaobolove/p/7684447.html