码迷,mamicode.com
首页 > Web开发 > 详细

nodejs 中on 和 emit

时间:2017-10-18 00:11:59      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:监听   who   xxxx   移除   code   col   listeners   nodejs   for   

首先测试用例:

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

nodejs 中on 和 emit

标签:监听   who   xxxx   移除   code   col   listeners   nodejs   for   

原文地址:http://www.cnblogs.com/yaobolove/p/7684447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!