标签:红色 logs 参数 eve 事件监听器 批量 用法 输出 可见
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(‘求安慰‘, ‘汉子‘);
---------------------------------------
触发 [求溺爱] 事件.
1 //触发: 2 life.emit(‘求安慰‘, ‘汉子‘); 3 4 life.emit(‘求溺爱‘, ‘妹子‘);
-----------------------------------------------------------------------
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 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);
可见 , [求安慰] 事件 注册了 监听器, [求溺爱]事件 没有注册 监听器.
默认是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(‘求溺爱‘, ‘妹子‘);
注意:
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(‘求溺爱‘, ‘妹子‘);
注意:
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(‘求溺爱‘, ‘妹子‘);
注意:
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);
可见 [求安慰] 没有注册事件.
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);
可见 , [求安慰],[求溺爱] 都没有 监听器.
标签:红色 logs 参数 eve 事件监听器 批量 用法 输出 可见
原文地址:http://www.cnblogs.com/cbza/p/7284364.html