User Tools

Site Tools


njsetn:handling_events_with_eventemitter

nodejs.org/api/events.html#events_class_events_eventemitter

“implementation of the pop sop design pattern” (sic ?)

on() → eventEmitter

Part of the event module

var events = require('events');
var emitter = new events.EventEmitter();
emitter.on('custom_event', function(message, status) {
  console.log(`{status}: ${message}`);
});

emitter.emit('custom_event', 'mon message', 200);

V2

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var Person = function(name) {
  this.name = name;
};

util.inherits(Person, EventEmitter); // on fait hériter person du contructeur EvenEmitter extrait plus haut du module events

var maurice = new Person("Maurice Yolo");

ben.on('speak', function(said) {
  console.log(`${this.name}: ${said}`);
});

ben.emit('speak', 'Bonjour.');
njsetn/handling_events_with_eventemitter.txt · Last modified: 2016/03/21 21:08 by leo