Node.js 事件触发器详细总结
<p><strong><span style="font-size: 14pt">Api目录</span></strong></p><p><span style="font-size: 18px"> 1、简单的注册、触发事件的流程</span></p>
<p><span style="font-size: 18px"> 2、eventEmitter.defaultMaxListeners</span></p>
<p><span style="font-size: 18px"> 3、emitter.setMaxListeners(n)</span></p>
<p><span style="font-size: 18px"> 4、emitter.getMaxListeners(n)</span></p>
<p><span style="font-size: 18px"> 5、emitter.prependListener(eventName, listener);</span></p>
<p><span style="font-size: 18px"> 6、emitter.prependOnceListener(eventName, listener)</span></p>
<p><span style="font-size: 18px"> 7、emitter.removeListener(eventName, listener)</span></p>
<p><span style="font-size: 18px"> 8、emitter.removeAllListeners(eventName)</span></p>
<p><span style="font-size: 18px"> 9、emitter.off(eventName, listener)</span></p>
<p><span style="font-size: 18px"> 10、emitter.eventNames()</span></p>
<p><span style="font-size: 18px"> 11、emitter.on('eventName', listener)</span></p>
<p><span style="font-size: 18px"> 12、emitter.once('eventName', listener)</span></p>
<p><span style="font-size: 18px"> 13、emitter.emit('eventName', params)</span></p>
<p> </p>
<p><strong><span style="font-size: 14pt">一、什么是事件模块(events)</span></strong></p>
<p>1、大多数Node.js核心Api都是采用惯用的异步事件驱动架构</p>
<p>2、所有能触发事件的对象都是EventEmitter类的实例</p>
<p>3、事件流程:引入模块 -> 实例化EventEmitter类 -> 注册事件 -> 触发事件</p>
<p> </p>
<p><strong><span style="font-size: 14pt">二、举例说明Api</span></strong></p>
<p><strong><span style="font-size: 18px">1、简单的注册、触发事件的流程</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入事件模块</span>
const eventEmitter= require('event'<span style="color: rgba(0, 0, 0, 1)">).EventEmitter;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 实例化EventEmitter类</span>
let emitter = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> eventEmitter();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注册事件</span>
emitter.on('someEvent', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
console.log(</span>'触发事件'<span style="color: rgba(0, 0, 0, 1)">)
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 触发事件</span>
emitter.emit('someEvent')</pre>
<pre>// 打印:触发事件</pre>
</div>
<p>注:node.js官方文档中,api的EventEmitter 和 emitter 在此所指:</p>
<p>a、所有 EventEmitter 对象指的就是 eventEmitter 对象</p>
<p>b、所有 emitter 实例,指的就是上面实例化的 emitter 对象</p>
<p> </p>
<p><strong><span style="font-size: 18px">2、eventEmitter.defaultMaxListeners</span></strong></p>
<p>EventEmitter下所有实例的可注册监听器个数的默认值. 默认是10个.(注:是改变EventEmitter下所有实例的);</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">console.log(eventEmitter.defaultMaxListeners);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印:10</span></pre>
</div>
<p> </p>
<p><span style="font-size: 18px"><strong>3、emitter.setMaxListeners(n)</strong></span></p>
<p>改变指定的单个 EventEmitter 实例的最大注册数量; 值设为 Infinity(或 0)表示不限制监听器的数量。</p>
<p><span style="font-size: 18px"><strong>4、emitter.getMaxListeners(n)</strong></span></p>
<p>获取单个实例最大监听器数量</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置单个实例最大监听器数量</span>
emitter.<span style="color: rgba(51, 102, 255, 1)">setMaxListeners</span>(15<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取单个实例最大监听器数量</span>
let getMaxListeners =<span style="color: rgba(0, 0, 0, 1)"> emitter.<span style="color: rgba(51, 102, 255, 1)">getMaxListeners</span>();
console.log(getMaxListeners);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印:15</span></pre>
</div>
<p> </p>
<p><strong><span style="font-size: 18px">5、emitter.prependListener(eventName, listener);</span></strong></p>
<p>eventName: 已经注册的监听器名称</p>
<p>listener: Function 回调</p>
<p>用来指定多个相同eventName的监听器中,首先调用哪个listener回调.(原理是将指定监听器,添加到监听器数组的开头,本质还是顺序执行)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> test1 listener监听方法</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> test1 () { console.log('a'<span style="color: rgba(0, 0, 0, 1)">) }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> test2 listener监听方法</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> test2 () { console.log('b'<span style="color: rgba(0, 0, 0, 1)">) }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 绑定foo事件</span>
emitter.on('foo'<span style="color: rgba(0, 0, 0, 1)">, test1);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将test2监听器方法放在监听器数组的头部,首先执行</span>
emitter.<span style="color: rgba(51, 102, 255, 1)">prependListener</span>('foo'<span style="color: rgba(0, 0, 0, 1)">, test2);
emitter.emit(</span>'foo'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印:'b' 'a'</span></pre>
</div>
<p> </p>
<p><strong><span style="font-size: 18px">6、emitter.prependOnceListener(eventName, listener)</span></strong></p>
<p>eventName: 已经注册的监听器名称</p>
<p>listener: Function 回调</p>
<p>添加一个只能执行一次的eventName事件监听器,下次再触发时,当前监听器会被先移除,然后在调用.</p>
<p>添加单次监听器 listener 到名为 eventName 的事件的监听器数组的开头 ( 当 eventName 事件第二次触发时,监听器会先被移除,然后再调用。)</p>
<p> </p>
<p><strong><span style="font-size: 18px">7、emitter.removeListener(eventName, listener)</span></strong></p>
<p>从名为 eventName 的事件的监听器数组中移除指定的 listener</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> removeListener:删除监听器触发</span>
emitter.on("removeListener", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (event, listener) {
console.log(</span>"移除了-" + event + "事件的" + listener "监听器"<span style="color: rgba(0, 0, 0, 1)">)
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 绑定test事件,并将tests作为test事件的listener</span>
emitter.on("test"<span style="color: rgba(0, 0, 0, 1)">, tests);
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> tests(){
console.log(</span>"测试事件"<span style="color: rgba(0, 0, 0, 1)">)
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 触发test事件</span>
emitter.emit("test"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除test的事件监听数组中的tests监听器</span>
emitter.removeListener("test"<span style="color: rgba(0, 0, 0, 1)">, tests);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印:</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 测试事件</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 移除了 test 事件的 tests 监听器</span></pre>
</div>
<p> </p>
<p><strong><span style="font-size: 18px">8、emitter.removeAllListeners(eventName)</span></strong></p>
<p>移除<span style="color: rgba(51, 102, 255, 1)">全部监听器</span>或指定的 eventName 事件的<span style="color: rgba(51, 102, 255, 1)">所有监听器</span>。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> test1事件 只有一个监听器</span>
emitter.on("test1", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (data) {
console.log(data)
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> test2事件 只有一个监听器</span>
emitter.on("test2", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (data) {
console.log(data)
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> test3 事件有两个监听器 分别是 test3s 和 test3ss</span>
emitter.on("test3"<span style="color: rgba(0, 0, 0, 1)">, test3s);
emitter.on(</span>"test3"<span style="color: rgba(0, 0, 0, 1)">, test3ss);
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test3s(data) {
console.log(data)
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test3ss(data) {
console.log(data)
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除监听器触发</span>
emitter.on("removeListener", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (event, listener) {
console.log(</span>"移除了-" + listener + "事件"<span style="color: rgba(0, 0, 0, 1)">)
});
emitter.emit(</span>"test1", '1'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
emitter.emit("test2", '2'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2</span>
emitter.emit("test3", '3'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3 3</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">移除test3下面所有的监听器</span>
emitter.<span style="color: rgba(51, 102, 255, 1)">removeAllListeners</span>('<span style="color: rgba(51, 102, 255, 1)">test3</span>'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一次:移除了test3s 第二次:移除了test3ss </span>
emitter.emit("test1", '4'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4</span>
emitter.emit("test2", '5'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5</span>
emitter.emit("test3", '6'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 无结果因为test3事件的监听器全部移除了</span></pre>
</div>
<p> </p>
<p><strong><span style="font-size: 18px">9、emitter.off(eventName, listener) </span></strong></p>
<p>removeListener的别名</p>
<p> </p>
<p><span style="font-size: 18px"><strong>10、emitter.eventNames()</strong></span></p>
<p>返回emitter实例下已注册的事件名数组。 数组中的值为字符串;</p>
<div class="cnblogs_code">
<pre>emitter.once('loadOnce', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
console.log();
});
emitter.on(</span>'load', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
console.log();
});
emitter.emit(</span>'load'<span style="color: rgba(0, 0, 0, 1)">);
console.log(emitter.eventNames());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印:[ 'loadOnce', 'load' ]</span>
<span style="color: rgba(0, 0, 0, 1)">
emitter.emit(</span>'loadOnce'<span style="color: rgba(0, 0, 0, 1)">);
console.log(emitter.eventNames());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印:['load' ]</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 原因: 因为once绑定的事件,执行一次之后就会移除。</span></pre>
</div>
<p> </p>
<p><span style="font-size: 18px"><strong>11、emitter.on('eventName', listener)</strong></span></p>
<p>eventName:事件名称</p>
<p>listener:回调函数</p>
<p>注册名称为eventName、回调函数为listener的事件</p>
<p> </p>
<p><strong><span style="font-size: 18px">12、emitter.once('eventName', listener)</span></strong></p>
<p>注册一次性名称为eventName,回调函数为listener的事件</p>
<p>注意:只触发一次,就会将其移除。</p>
<p> </p>
<p><strong><span style="font-size: 18px">13、emitter.emit('eventName', params)</span></strong></p>
<p>params:any 任意类型</p>
<p>触发名称为eventName的事件,按监听器数组顺序执行所有回调函数。</p>
<p> </p>
<p><strong><span style="font-size: 18pt">注意:以上nodejs事件的api,都是作者辛苦练习总结出来的,转载需注明转载地址,谢谢配合!</span></strong></p>
<p> </p>
<p><strong style="font-size: 18px">4、emitter.getMaxListeners(n)</strong></p><br><br>
来源:https://www.cnblogs.com/wangweizhang/p/10901605.html
頁:
[1]