首页 > 代码库 > nodejs -- event 模块, 事件模块.
nodejs -- event 模块, 事件模块.
1. 注册事件 on 或者 addListener,触发事件 emit
1-1简单的使用:
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 //求安慰: 6 7 8 function water(who){ 9 console.log(‘给 ‘ +who+ ‘ 倒水‘); 10 } 11 //具名函数 12 life.addListener(‘求安慰‘, water); 13 14 //匿名函数 15 life.addListener(‘求安慰‘, function(who){ 16 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 17 }); 18 19 20 function cook(who){ 21 console.log(‘给 ‘ +who+ ‘ 做饭‘); 22 } 23 //具名函数 24 life.on(‘求安慰‘, cook); 25 //匿名函数 26 life.on(‘求安慰‘, function(who){ 27 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 28 }); 29 30 life.on(‘求安慰‘, function(who){ 31 console.log(‘给 ‘ +who+ ‘ .....5‘); 32 }); 33 34 life.on(‘求安慰‘, function(who){ 35 console.log(‘给 ‘ +who+ ‘ .....6‘); 36 }); 37 38 life.on(‘求安慰‘, function(who){ 39 console.log(‘给 ‘ +who+ ‘ .....7‘); 40 }); 41 42 life.on(‘求安慰‘, function(who){ 43 console.log(‘给 ‘ +who+ ‘ .....8‘); 44 }); 45 46 life.on(‘求安慰‘, function(who){ 47 console.log(‘给 ‘ +who+ ‘ .....9‘); 48 }); 49 50 51 //求溺爱 52 53 life.on(‘求溺爱‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 55 }); 56 57 life.on(‘求溺爱‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ 看电影1‘); 59 }); 60 61 life.on(‘求溺爱‘, function(who){ 62 console.log(‘给 ‘ +who+ ‘ 去旅游1‘); 63 }); 64 65 //触发: 66 life.emit(‘求安慰‘, ‘汉子‘);
- 这里一共有两个事件, 求安慰, 一共有 9个动作; 求溺爱 ,一共有 3个动作
- 添加事件监听 有 addListener() 和 on() 两种方法, 这两种方法都 可以 使用 具名函数 和 匿名函数.
- emit() 触发事件, 上面 没有触发 [求溺爱]事件, 因此没有输出.
---------------------------------------
触发 [求溺爱] 事件.
1 //触发: 2 life.emit(‘求安慰‘, ‘汉子‘); 3 4 life.emit(‘求溺爱‘, ‘妹子‘);
-----------------------------------------------------------------------
1-2emit(event, 参数1, 参数2,参数3) 传递多个参数的时候:
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 6 7 //求安慰: 8 9 10 function water(who){ 11 console.log(‘给 ‘ +who+ ‘ 倒水1‘); 12 } 13 //具名函数 14 life.addListener(‘求安慰‘, water); 15 16 //匿名函数 17 life.addListener(‘求安慰‘, function(who){ 18 console.log(‘给 ‘ +who+ ‘ 揉肩2‘); 19 }); 20 21 22 function cook(who){ 23 console.log(‘给 ‘ +who+ ‘ 做饭3‘); 24 } 25 //具名函数 26 life.on(‘求安慰‘, cook); 27 //匿名函数 28 life.on(‘求安慰‘, function(who){ 29 console.log(‘给 ‘ +who+ ‘ 洗衣服4‘); 30 }); 31 32 life.on(‘求安慰‘, function(){ 33 console.log(‘这个是 第 5个 监听器‘); 34 }); 35 36 life.on(‘求安慰‘, function(address, age){ 37 console.log(‘我要去‘ +address+ ‘旅游,我的年龄是‘ +age ); 38 }); 39 40 41 42 43 44 //触发: 45 life.emit(‘求安慰‘, ‘汉子‘, ‘北京‘, 26);
红色部分显然是不符合我们的要求的.
---
解决办法: 使得 36行是 3个参数, 但是 第一个参数不使用 .
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 6 7 //求安慰: 8 9 10 function water(who){ 11 console.log(‘给 ‘ +who+ ‘ 倒水1‘); 12 } 13 //具名函数 14 life.addListener(‘求安慰‘, water); 15 16 //匿名函数 17 life.addListener(‘求安慰‘, function(who){ 18 console.log(‘给 ‘ +who+ ‘ 揉肩2‘); 19 }); 20 21 22 function cook(who){ 23 console.log(‘给 ‘ +who+ ‘ 做饭3‘); 24 } 25 //具名函数 26 life.on(‘求安慰‘, cook); 27 //匿名函数 28 life.on(‘求安慰‘, function(who){ 29 console.log(‘给 ‘ +who+ ‘ 洗衣服4‘); 30 }); 31 32 life.on(‘求安慰‘, function(){ 33 console.log(‘这个是 第 5个 监听器‘); 34 }); 35 36 life.on(‘求安慰‘, function(who, address, age){ 37 console.log(‘我要去‘ +address+ ‘旅游,我的年龄是‘ +age ); 38 }); 39 40 41 42 43 44 //触发: 45 life.emit(‘求安慰‘, ‘汉子‘, ‘北京‘, 26);
1-3:如何判断 事件有没有注册 监听器?
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 6 7 //求安慰: 8 9 10 function water(who){ 11 console.log(‘给 ‘ +who+ ‘ 倒水1‘); 12 } 13 //具名函数 14 life.addListener(‘求安慰‘, water); 15 16 //匿名函数 17 life.addListener(‘求安慰‘, function(who){ 18 console.log(‘给 ‘ +who+ ‘ 揉肩2‘); 19 }); 20 21 22 function cook(who){ 23 console.log(‘给 ‘ +who+ ‘ 做饭3‘); 24 } 25 //具名函数 26 life.on(‘求安慰‘, cook); 27 //匿名函数 28 life.on(‘求安慰‘, function(who){ 29 console.log(‘给 ‘ +who+ ‘ 洗衣服4‘); 30 }); 31 32 life.on(‘求安慰‘, function(){ 33 console.log(‘这个是 第 5个 监听器‘); 34 }); 35 36 life.on(‘求安慰‘, function(who, address, age){ 37 console.log(‘我要去‘ +address+ ‘旅游,我的年龄是‘ +age ); 38 }); 39 40 41 42 43 44 //触发: 45 var anwei = life.emit(‘求安慰‘, ‘汉子‘, ‘北京‘, 26); 46 47 var niai = life.emit(‘求溺爱‘); 48 49 console.log(anwei); 50 console.log(niai);
可见 , [求安慰] 事件 注册了 监听器, [求溺爱]事件 没有注册 监听器.
2. 设置事件 可以拥有的监听器数量.setMaxListeners(n)
默认是10个:
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 //求安慰: 6 7 8 function water(who){ 9 console.log(‘给 ‘ +who+ ‘ 倒水‘); 10 } 11 //具名函数 12 life.addListener(‘求安慰‘, water); 13 14 //匿名函数 15 life.addListener(‘求安慰‘, function(who){ 16 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 17 }); 18 19 20 function cook(who){ 21 console.log(‘给 ‘ +who+ ‘ 做饭‘); 22 } 23 //具名函数 24 life.on(‘求安慰‘, cook); 25 //匿名函数 26 life.on(‘求安慰‘, function(who){ 27 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 28 }); 29 30 life.on(‘求安慰‘, function(who){ 31 console.log(‘给 ‘ +who+ ‘ .....5‘); 32 }); 33 34 life.on(‘求安慰‘, function(who){ 35 console.log(‘给 ‘ +who+ ‘ .....6‘); 36 }); 37 38 life.on(‘求安慰‘, function(who){ 39 console.log(‘给 ‘ +who+ ‘ .....7‘); 40 }); 41 42 life.on(‘求安慰‘, function(who){ 43 console.log(‘给 ‘ +who+ ‘ .....8‘); 44 }); 45 46 life.on(‘求安慰‘, function(who){ 47 console.log(‘给 ‘ +who+ ‘ .....9‘); 48 }); 49 50 life.on(‘求安慰‘, function(who){ 51 console.log(‘给 ‘ +who+ ‘ .....10‘); 52 }); 53 54 life.on(‘求安慰‘, function(who){ 55 console.log(‘给 ‘ +who+ ‘ .....11‘); 56 }); 57 58 59 //求溺爱 60 61 life.on(‘求溺爱‘, function(who){ 62 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 63 }); 64 65 life.on(‘求溺爱‘, function(who){ 66 console.log(‘给 ‘ +who+ ‘ 看电影1‘); 67 }); 68 69 life.on(‘求溺爱‘, function(who){ 70 console.log(‘给 ‘ +who+ ‘ 去旅游1‘); 71 }); 72 73 //触发: 74 life.emit(‘求安慰‘, ‘汉子‘); 75 76 life.emit(‘求溺爱‘, ‘妹子‘);
--------------------------------------
增加 事件 的 最大监听器数量为 11.
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 // 设置事件 允许 的 最多 监听器 的数量 6 life.setMaxListeners(11); 7 8 //求安慰: 9 10 11 function water(who){ 12 console.log(‘给 ‘ +who+ ‘ 倒水‘); 13 } 14 //具名函数 15 life.addListener(‘求安慰‘, water); 16 17 //匿名函数 18 life.addListener(‘求安慰‘, function(who){ 19 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 20 }); 21 22 23 function cook(who){ 24 console.log(‘给 ‘ +who+ ‘ 做饭‘); 25 } 26 //具名函数 27 life.on(‘求安慰‘, cook); 28 //匿名函数 29 life.on(‘求安慰‘, function(who){ 30 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 31 }); 32 33 life.on(‘求安慰‘, function(who){ 34 console.log(‘给 ‘ +who+ ‘ .....5‘); 35 }); 36 37 life.on(‘求安慰‘, function(who){ 38 console.log(‘给 ‘ +who+ ‘ .....6‘); 39 }); 40 41 life.on(‘求安慰‘, function(who){ 42 console.log(‘给 ‘ +who+ ‘ .....7‘); 43 }); 44 45 life.on(‘求安慰‘, function(who){ 46 console.log(‘给 ‘ +who+ ‘ .....8‘); 47 }); 48 49 life.on(‘求安慰‘, function(who){ 50 console.log(‘给 ‘ +who+ ‘ .....9‘); 51 }); 52 53 life.on(‘求安慰‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ .....10‘); 55 }); 56 57 life.on(‘求安慰‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ .....11‘); 59 }); 60 61 62 //求溺爱 63 64 life.on(‘求溺爱‘, function(who){ 65 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 66 }); 67 68 life.on(‘求溺爱‘, function(who){ 69 console.log(‘给 ‘ +who+ ‘ 看电影2‘); 70 }); 71 72 life.on(‘求溺爱‘, function(who){ 73 console.log(‘给 ‘ +who+ ‘ 去旅游3‘); 74 }); 75 76 life.on(‘求溺爱‘, function(who){ 77 console.log(‘给 ‘ +who+ ‘ .....4‘); 78 }); 79 80 life.on(‘求溺爱‘, function(who){ 81 console.log(‘给 ‘ +who+ ‘ .....5‘); 82 }); 83 84 life.on(‘求溺爱‘, function(who){ 85 console.log(‘给 ‘ +who+ ‘ .....6‘); 86 }); 87 88 life.on(‘求溺爱‘, function(who){ 89 console.log(‘给 ‘ +who+ ‘ .....7‘); 90 }); 91 92 life.on(‘求溺爱‘, function(who){ 93 console.log(‘给 ‘ +who+ ‘ .....8‘); 94 }); 95 96 97 life.on(‘求溺爱‘, function(who){ 98 console.log(‘给 ‘ +who+ ‘ .....9‘); 99 }); 100 101 102 life.on(‘求溺爱‘, function(who){ 103 console.log(‘给 ‘ +who+ ‘ .....10‘); 104 }); 105 106 107 life.on(‘求溺爱‘, function(who){ 108 console.log(‘给 ‘ +who+ ‘ .....11‘); 109 }); 110 111 112 113 114 //触发: 115 life.emit(‘求安慰‘, ‘汉子‘); 116 117 life.emit(‘求溺爱‘, ‘妹子‘);
注意:
- life.setMaxListeners(11); 在添加事件之前 设置 允许 的 事件 监听器 数量是 11个.
- 添加的事件监听器 数量 是 针对 所有的事件 生效的; 本 脚本内, 有两个事件, [求安慰] [求溺爱] 都 有了 11个 监听器.
3. 移除单个: 移除某个事件 的 某一个监听器. removeListener(event, listener)
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 // 设置事件 允许 的 最多 监听器 的数量 6 life.setMaxListeners(11); 7 8 //求安慰: 9 10 11 function water(who){ 12 console.log(‘给 ‘ +who+ ‘ 倒水‘); 13 } 14 //具名函数 15 life.addListener(‘求安慰‘, water); 16 17 //匿名函数 18 life.addListener(‘求安慰‘, function(who){ 19 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 20 }); 21 22 23 function cook(who){ 24 console.log(‘给 ‘ +who+ ‘ 做饭‘); 25 } 26 //具名函数 27 life.on(‘求安慰‘, cook); 28 //匿名函数 29 life.on(‘求安慰‘, function(who){ 30 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 31 }); 32 33 life.on(‘求安慰‘, function(who){ 34 console.log(‘给 ‘ +who+ ‘ .....5‘); 35 }); 36 37 life.on(‘求安慰‘, function(who){ 38 console.log(‘给 ‘ +who+ ‘ .....6‘); 39 }); 40 41 life.on(‘求安慰‘, function(who){ 42 console.log(‘给 ‘ +who+ ‘ .....7‘); 43 }); 44 45 life.on(‘求安慰‘, function(who){ 46 console.log(‘给 ‘ +who+ ‘ .....8‘); 47 }); 48 49 life.on(‘求安慰‘, function(who){ 50 console.log(‘给 ‘ +who+ ‘ .....9‘); 51 }); 52 53 life.on(‘求安慰‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ .....10‘); 55 }); 56 57 life.on(‘求安慰‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ .....11‘); 59 }); 60 61 62 //求溺爱 63 64 life.on(‘求溺爱‘, function(who){ 65 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 66 }); 67 68 life.on(‘求溺爱‘, function(who){ 69 console.log(‘给 ‘ +who+ ‘ 看电影2‘); 70 }); 71 72 life.on(‘求溺爱‘, function(who){ 73 console.log(‘给 ‘ +who+ ‘ 去旅游3‘); 74 }); 75 76 life.on(‘求溺爱‘, function(who){ 77 console.log(‘给 ‘ +who+ ‘ .....4‘); 78 }); 79 80 life.on(‘求溺爱‘, function(who){ 81 console.log(‘给 ‘ +who+ ‘ .....5‘); 82 }); 83 84 life.on(‘求溺爱‘, function(who){ 85 console.log(‘给 ‘ +who+ ‘ .....6‘); 86 }); 87 88 life.on(‘求溺爱‘, function(who){ 89 console.log(‘给 ‘ +who+ ‘ .....7‘); 90 }); 91 92 life.on(‘求溺爱‘, function(who){ 93 console.log(‘给 ‘ +who+ ‘ .....8‘); 94 }); 95 96 97 life.on(‘求溺爱‘, function(who){ 98 console.log(‘给 ‘ +who+ ‘ .....9‘); 99 }); 100 101 102 life.on(‘求溺爱‘, function(who){ 103 console.log(‘给 ‘ +who+ ‘ .....10‘); 104 }); 105 106 107 life.on(‘求溺爱‘, function(who){ 108 console.log(‘给 ‘ +who+ ‘ .....11‘); 109 }); 110 111 112 life.removeListener(‘求安慰‘, water); 113 114 //触发: 115 life.emit(‘求安慰‘, ‘汉子‘); 116 117 life.emit(‘求溺爱‘, ‘妹子‘);
注意:
- 上面移除了 water 对应的 [倒水]事件.
- 这个函数只能移除 单个事件的 某个监听器
- 不可以移除 所有的事件 的 监听器
- 不可以移除 单个事件的 所有监听器, removeListen(‘求安慰‘) , 这种用法 会 报错
- 这个函数 只能 移除 具名函数 对应 的监听器 ; 匿名函数 注册的监听器 无法移除.
4. 判断 事件拥有的 监听器 的数量 listeners(event) , listenerCount(emitter, event)
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 // 设置事件 允许 的 最多 监听器 的数量 6 life.setMaxListeners(11); 7 8 //求安慰: 9 10 11 function water(who){ 12 console.log(‘给 ‘ +who+ ‘ 倒水‘); 13 } 14 //具名函数 15 life.addListener(‘求安慰‘, water); 16 17 //匿名函数 18 life.addListener(‘求安慰‘, function(who){ 19 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 20 }); 21 22 23 function cook(who){ 24 console.log(‘给 ‘ +who+ ‘ 做饭‘); 25 } 26 //具名函数 27 life.on(‘求安慰‘, cook); 28 //匿名函数 29 life.on(‘求安慰‘, function(who){ 30 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 31 }); 32 33 life.on(‘求安慰‘, function(who){ 34 console.log(‘给 ‘ +who+ ‘ .....5‘); 35 }); 36 37 life.on(‘求安慰‘, function(who){ 38 console.log(‘给 ‘ +who+ ‘ .....6‘); 39 }); 40 41 life.on(‘求安慰‘, function(who){ 42 console.log(‘给 ‘ +who+ ‘ .....7‘); 43 }); 44 45 life.on(‘求安慰‘, function(who){ 46 console.log(‘给 ‘ +who+ ‘ .....8‘); 47 }); 48 49 life.on(‘求安慰‘, function(who){ 50 console.log(‘给 ‘ +who+ ‘ .....9‘); 51 }); 52 53 life.on(‘求安慰‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ .....10‘); 55 }); 56 57 life.on(‘求安慰‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ .....11‘); 59 }); 60 61 62 //求溺爱 63 64 life.on(‘求溺爱‘, function(who){ 65 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 66 }); 67 68 life.on(‘求溺爱‘, function(who){ 69 console.log(‘给 ‘ +who+ ‘ 看电影2‘); 70 }); 71 72 life.on(‘求溺爱‘, function(who){ 73 console.log(‘给 ‘ +who+ ‘ 去旅游3‘); 74 }); 75 76 life.on(‘求溺爱‘, function(who){ 77 console.log(‘给 ‘ +who+ ‘ .....4‘); 78 }); 79 80 life.on(‘求溺爱‘, function(who){ 81 console.log(‘给 ‘ +who+ ‘ .....5‘); 82 }); 83 84 life.on(‘求溺爱‘, function(who){ 85 console.log(‘给 ‘ +who+ ‘ .....6‘); 86 }); 87 88 life.on(‘求溺爱‘, function(who){ 89 console.log(‘给 ‘ +who+ ‘ .....7‘); 90 }); 91 92 life.on(‘求溺爱‘, function(who){ 93 console.log(‘给 ‘ +who+ ‘ .....8‘); 94 }); 95 96 97 life.on(‘求溺爱‘, function(who){ 98 console.log(‘给 ‘ +who+ ‘ .....9‘); 99 }); 100 101 102 life.on(‘求溺爱‘, function(who){ 103 console.log(‘给 ‘ +who+ ‘ .....10‘); 104 }); 105 106 107 life.on(‘求溺爱‘, function(who){ 108 console.log(‘给 ‘ +who+ ‘ .....11‘); 109 }); 110 111 console.log(‘移除前:‘); 112 console.log(life.listeners().length); 113 console.log(life.listeners(‘求安慰‘).length); 114 115 console.log(EventEmitter.listenerCount(life, ‘求安慰‘)); 116 117 life.removeListener(‘求安慰‘, water); 118 119 console.log(‘移除后:‘); 120 console.log(life.listeners().length); 121 console.log(life.listeners(‘求安慰‘).length); 122 123 console.log(EventEmitter.listenerCount(life, ‘求安慰‘)); 124 125 //触发: 126 // life.emit(‘求安慰‘, ‘汉子‘); 127 128 // life.emit(‘求溺爱‘, ‘妹子‘);
注意:
- EventEmitter类的实例.listeners(事件名).length 和 EventEmitter.listenerCount(实例名, 事件名) 都可以获得 对应的实例中 的 对应 的事件 绑定 的监听器数量
- EventEmitter类的实例.listeners().length , 如果没有传递 事件名 , 数量就是0 ; 可见 必须传递事件名
- 移除water之前 , [求安慰] 的监听器 是 11个 , 移除water之后, [求安慰] 的监听器 数量 是 10个.
5. 批量移除 某个实例中的事件绑定 的 监听器 : removeAllListeners([event])
5-1: 批量移除 某个实例 中的 某个事件 的所有 监听器:
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 // 设置事件 允许 的 最多 监听器 的数量 6 life.setMaxListeners(11); 7 8 //求安慰: 9 10 11 function water(who){ 12 console.log(‘给 ‘ +who+ ‘ 倒水‘); 13 } 14 //具名函数 15 life.addListener(‘求安慰‘, water); 16 17 //匿名函数 18 life.addListener(‘求安慰‘, function(who){ 19 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 20 }); 21 22 23 function cook(who){ 24 console.log(‘给 ‘ +who+ ‘ 做饭‘); 25 } 26 //具名函数 27 life.on(‘求安慰‘, cook); 28 //匿名函数 29 life.on(‘求安慰‘, function(who){ 30 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 31 }); 32 33 life.on(‘求安慰‘, function(who){ 34 console.log(‘给 ‘ +who+ ‘ .....5‘); 35 }); 36 37 life.on(‘求安慰‘, function(who){ 38 console.log(‘给 ‘ +who+ ‘ .....6‘); 39 }); 40 41 life.on(‘求安慰‘, function(who){ 42 console.log(‘给 ‘ +who+ ‘ .....7‘); 43 }); 44 45 life.on(‘求安慰‘, function(who){ 46 console.log(‘给 ‘ +who+ ‘ .....8‘); 47 }); 48 49 life.on(‘求安慰‘, function(who){ 50 console.log(‘给 ‘ +who+ ‘ .....9‘); 51 }); 52 53 life.on(‘求安慰‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ .....10‘); 55 }); 56 57 life.on(‘求安慰‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ .....11‘); 59 }); 60 61 62 //求溺爱 63 64 life.on(‘求溺爱‘, function(who){ 65 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 66 }); 67 68 life.on(‘求溺爱‘, function(who){ 69 console.log(‘给 ‘ +who+ ‘ 看电影2‘); 70 }); 71 72 life.on(‘求溺爱‘, function(who){ 73 console.log(‘给 ‘ +who+ ‘ 去旅游3‘); 74 }); 75 76 life.on(‘求溺爱‘, function(who){ 77 console.log(‘给 ‘ +who+ ‘ .....4‘); 78 }); 79 80 life.on(‘求溺爱‘, function(who){ 81 console.log(‘给 ‘ +who+ ‘ .....5‘); 82 }); 83 84 life.on(‘求溺爱‘, function(who){ 85 console.log(‘给 ‘ +who+ ‘ .....6‘); 86 }); 87 88 life.on(‘求溺爱‘, function(who){ 89 console.log(‘给 ‘ +who+ ‘ .....7‘); 90 }); 91 92 life.on(‘求溺爱‘, function(who){ 93 console.log(‘给 ‘ +who+ ‘ .....8‘); 94 }); 95 96 97 life.on(‘求溺爱‘, function(who){ 98 console.log(‘给 ‘ +who+ ‘ .....9‘); 99 }); 100 101 102 life.on(‘求溺爱‘, function(who){ 103 console.log(‘给 ‘ +who+ ‘ .....10‘); 104 }); 105 106 107 life.on(‘求溺爱‘, function(who){ 108 console.log(‘给 ‘ +who+ ‘ .....11‘); 109 }); 110 111 life.removeAllListeners(‘求安慰‘); 112 113 114 //触发: 115 var anwei = life.emit(‘求安慰‘, ‘汉子‘); 116 var niai = life.emit(‘求溺爱‘, ‘妹子‘); 117 118 console.log(anwei); 119 console.log(niai);
可见 [求安慰] 没有注册事件.
5-2: 批量移除 某个实例 中 的所有事件 的 监听器:
1 var EventEmitter = require(‘events‘).EventEmitter; 2 3 var life = new EventEmitter(); 4 5 // 设置事件 允许 的 最多 监听器 的数量 6 life.setMaxListeners(11); 7 8 //求安慰: 9 10 11 function water(who){ 12 console.log(‘给 ‘ +who+ ‘ 倒水‘); 13 } 14 //具名函数 15 life.addListener(‘求安慰‘, water); 16 17 //匿名函数 18 life.addListener(‘求安慰‘, function(who){ 19 console.log(‘给 ‘ +who+ ‘ 揉肩‘); 20 }); 21 22 23 function cook(who){ 24 console.log(‘给 ‘ +who+ ‘ 做饭‘); 25 } 26 //具名函数 27 life.on(‘求安慰‘, cook); 28 //匿名函数 29 life.on(‘求安慰‘, function(who){ 30 console.log(‘给 ‘ +who+ ‘ 洗衣服‘); 31 }); 32 33 life.on(‘求安慰‘, function(who){ 34 console.log(‘给 ‘ +who+ ‘ .....5‘); 35 }); 36 37 life.on(‘求安慰‘, function(who){ 38 console.log(‘给 ‘ +who+ ‘ .....6‘); 39 }); 40 41 life.on(‘求安慰‘, function(who){ 42 console.log(‘给 ‘ +who+ ‘ .....7‘); 43 }); 44 45 life.on(‘求安慰‘, function(who){ 46 console.log(‘给 ‘ +who+ ‘ .....8‘); 47 }); 48 49 life.on(‘求安慰‘, function(who){ 50 console.log(‘给 ‘ +who+ ‘ .....9‘); 51 }); 52 53 life.on(‘求安慰‘, function(who){ 54 console.log(‘给 ‘ +who+ ‘ .....10‘); 55 }); 56 57 life.on(‘求安慰‘, function(who){ 58 console.log(‘给 ‘ +who+ ‘ .....11‘); 59 }); 60 61 62 //求溺爱 63 64 life.on(‘求溺爱‘, function(who){ 65 console.log(‘给 ‘ +who+ ‘ 买包包1‘); 66 }); 67 68 life.on(‘求溺爱‘, function(who){ 69 console.log(‘给 ‘ +who+ ‘ 看电影2‘); 70 }); 71 72 life.on(‘求溺爱‘, function(who){ 73 console.log(‘给 ‘ +who+ ‘ 去旅游3‘); 74 }); 75 76 life.on(‘求溺爱‘, function(who){ 77 console.log(‘给 ‘ +who+ ‘ .....4‘); 78 }); 79 80 life.on(‘求溺爱‘, function(who){ 81 console.log(‘给 ‘ +who+ ‘ .....5‘); 82 }); 83 84 life.on(‘求溺爱‘, function(who){ 85 console.log(‘给 ‘ +who+ ‘ .....6‘); 86 }); 87 88 life.on(‘求溺爱‘, function(who){ 89 console.log(‘给 ‘ +who+ ‘ .....7‘); 90 }); 91 92 life.on(‘求溺爱‘, function(who){ 93 console.log(‘给 ‘ +who+ ‘ .....8‘); 94 }); 95 96 97 life.on(‘求溺爱‘, function(who){ 98 console.log(‘给 ‘ +who+ ‘ .....9‘); 99 }); 100 101 102 life.on(‘求溺爱‘, function(who){ 103 console.log(‘给 ‘ +who+ ‘ .....10‘); 104 }); 105 106 107 life.on(‘求溺爱‘, function(who){ 108 console.log(‘给 ‘ +who+ ‘ .....11‘); 109 }); 110 111 // life.removeAllListeners(‘求安慰‘); 112 life.removeAllListeners(); 113 114 115 //触发: 116 var anwei = life.emit(‘求安慰‘, ‘汉子‘); 117 var niai = life.emit(‘求溺爱‘, ‘妹子‘); 118 119 console.log(anwei); 120 console.log(niai);
可见 , [求安慰],[求溺爱] 都没有 监听器.
参考链接:
- Node.js EventEmitter
- Node.js 事件模块小插曲
nodejs -- event 模块, 事件模块.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。