首先测试用例:
var EventEmitter = require('events').EventEmitter var 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('事件名')--- 相当于触发事件
*/