Nodejs's Event module
There is an on (' Data ') event in the crawler code, why does the response res have an on method, what does this on do, and to answer these questions, from the event module, there is no browser bubbling in the Nodejs, capturing these behaviors, So this module, which is implemented in Nodejs, contains most of the modules that are integrated into this module,
So events is the most important module, it only exposes an object, that is, eventimit, the role of two is the launch of the event, and the monitoring of the event, two different execution links would have been unrelated, but with the support of the event module, the two links can establish a link, Eventimit supports multiple event listeners with a maximum value of 10,
is to add 10 event listeners to an event;
var eventemitter = require (' Events '). Eventemittervar life = new Eventemitter ()//addeventlistenerlife.on (' Seek comfort ', function (WHO) {Console.log (' Give ' +who+ ')}) Life.on (' seek comfort ', function (WHO) {Console.log (' to ' +who+ ' rub Shoulders ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' Give ' +who+ ' cooking ')}) Life.on (' comfort ', function (WHO) {Console.log (' to ' +who+ ' laundry ')}) Life.on (' Seeking comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 5 ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 6 ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 7 ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 8 ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 9 ')}) Life.on (' Seek comfort ', function (WHO) {Console.log (' to ' +who+ ' ... 10 ')})//Trigger Life.emit (' seek comfort ', ' man ')
The results of the operation are as follows:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/8F/2C/wKioL1jWJtPyQfnrAAA2bFT_Av0498.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwjtpyqfnraaa2bft_av0498.jpg "/>
What happens if there are more than 10 bound events?
Life.on (' comfort ', function (WHO) {Console.log (' Give ' +who+ ' you want to die For me ')})
The results of the implementation are as follows:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/8F/2E/wKiom1jWJ7LADgjrAAB7vb7EkAo515.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jwj7ladgjraab7vb7ekao515.jpg "/>
The 11th listener will still execute, but will throw a warning, which is officially recommended, for an event set not more than 10 listeners, too many words will lead to memory leakage, of course, this value can be modified,
Let's change the limit that the event endures:
Life.setmaxlisteners (11)//After adding the new object, on before
The following results are performed:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/8F/2C/wKioL1jWKbGRoZtwAABBs77uJsw372.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwkbgroztwaabbs77ujsw372.jpg "/>
If I register another event with life, will it occupy this maximum value?
The code is as follows:
Life.on (' Seeking care ', function (WHO) {Console.log (' to ' +who+ ' buy Clothes ')}) Life.on (' Seeking care ', function (WHO) {Console.log (' Give ' +who+ ' pay ') })
The results of the operation are as follows:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/8F/2C/wKioL1jWKbGRoZtwAABBs77uJsw372.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwkbgroztwaabbs77ujsw372.jpg "/>
From the running results found: if there is no emit to seek care events will not trigger the listener function,
It is not distributed uniformly for all events, but only 11 credits are allocated for the consolation event.
See if an event has been monitored?
The return value based on the listener event is a Boolean type
var Flag1 = Life.emit (' Seeking comfort ', ' man ') var flag2 = Life.emit (' Seeking care ', ' sister ') var flag3 = life.emit (' What ', ' Man and Man ') Console.log (Flag1 ); Console.log (Flag2); Console.log (FLAG3);
The results of the operation are as follows:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/8F/2C/wKioL1jWLn3BheupAAAHt-43uz0861.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwln3bheupaaaht-43uz0861.jpg "/>
How to remove an event?
Wrong wording: Write before event trigger emit
Life.removelistener (' seek comfort ', function () {console.log (' Give ' +who+ ')})
The results of the operation are as follows:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/8F/2E/wKiom1jWMBHAsKMGAAAGxWIWAvg625.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jwmbhaskmgaaagxwiwavg625.jpg "/>
The description does not work.
So what should we write about? Anonymous functions are not possible in this form and need to write a named function.
The transformation is as follows:
function Water (WHO) {Console.log (' Give ' +who+ ')}//addeventlistenerlife.on (' Seek comfort ', water)//Remove Life.removelistener (' Seek comfort, water)
The results of the operation are as follows:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/8F/2C/wKioL1jWNxaSzxwWAABHf3rD9Mk957.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwnxaszxwwaabhf3rd9mk957.jpg "/>
The removal event was successful.
Query the remaining number of event listeners?
Console.log (life.listeners (' comfort '). Length) Console.log (Eventemitter.listenercount (life, ' seeking Comfort '))
The results are as follows:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/8F/2C/wKioL1jWOFjg4oP3AAAKT9oLcOo244.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jwofjg4op3aaakt9olcoo244.jpg "/>
Remove all listening: including seeking care, seeking comfort
Life.removealllisteners ()
The results of the operation are as follows:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/8F/2E/wKiom1jWONHxXjNvAAAJuwT06yk374.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jwonhxxjnvaaajuwt06yk374.jpg "/>
If you only want to remove the consolation, this time you need to pass the name of the specific event,
For example:
Life.removealllisteners (' seeking comfort ')
The results of the operation are as follows:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/8F/2E/wKiom1jWOkfyiMNJAAAJ09iEthc288.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jwokfyimnjaaaj09iethc288.jpg "/>
This article is from the "It Rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.com/3174958/1910338
node. JS (10)--nodejs Event