What is an “event emitter”?

前端 未结 4 1487
情歌与酒
情歌与酒 2020-12-12 13:04

Browsing through http://microjs.com, I see lots of libraries labelled \"event emitters\". I like to think I know my way around the basics of the Javascript language pretty

4条回答
  •  粉色の甜心
    2020-12-12 13:19

    Simple example in Node.js:

    var EventEmitter = require('events').EventEmitter;
    var concert = new EventEmitter;
    var singer = 'Coldplay';
    
    concert.on('start', function (singer) {
      console.log(`OMG ${singer}!`);
    });
    
    concert.on('finish', function () {
      console.log(`It was the best concert in my life...`);
    });
    
    concert.emit('start', singer);
    concert.emit('finish');
    

提交回复
热议问题