Nodejs Event Module Small instance

Source: Internet
Author: User


Very intuitive small example bar, put in the file node, or quite interesting

/** * Created by Amy on 2017/7/13. */var eventemitter=require (' Events '). Eventemittervar life=new Eventemitter () life.setmaxlisteners (4)//system defaults to no more than 10, prevent event leaks//addeventlistenerfunction Water (WHO) {    console.log (' Give ' +who+ ' pour water ')}life.on (' Seeking comfort ', function (WHO)) Life.on (' Seeking comfort ', function (WHO) {    Console.log (' Cook ' for ' +who+ ')}) Life.on (' Comfort ', function (WHO) {    console.log (' Give ' +who+ ' laundry ')}) Life.on (' Seeking indulgence ', function (WHO) {    console.log (' Give ' +who+ ' buy Clothes ')})//life.removelistener (' Seek comfort ', water)  //Remove Listener Events//life.removealllistener (' Seek comfort '//sister unhappy, required to remove all Var hasconfortlistener=life.emit (' Seeking comfort ', ' man ') var haslovedlistener=life.emit (' Seeking indulgence ', ' sister ') Console.log (life.listeners (' comfort '). Length) Console.log (Life.listeners (' Seeking indulgence '). Length) Console.log ( Eventemitter.listenercount (Life, ' comfort '))//Both can print out the number of listener events Console.log (Hasconfortlistener) Console.log ( Haslovedlistener)

  

Nodejs Event Module Small instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.