nodejs 中on 和 emit


首先测试用例:

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('事件名')--- 相当于触发事件
*/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM