Skip to content

Instantly share code, notes, and snippets.

@MelodicCrypter
Created August 19, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelodicCrypter/1cab1634ef3db1a2c840f0e9c7003cdf to your computer and use it in GitHub Desktop.
Save MelodicCrypter/1cab1634ef3db1a2c840f0e9c7003cdf to your computer and use it in GitHub Desktop.
NodeJS advanced EventEmitter.
const EventEmitter = require('events').EventEmitter;
const util = require('util');
// constructor function
const Person = function(name) {
this.name = name;
};
// let Person inherit all the EventEmitter features
util.inherits(Person, EventEmitter);
const ben = new Person("Ben");
// event listener
ben.on('speak', function(said) {
console.log(`${this.name}, said: ${said}`);
});
// sample emit
ben.emit('speak', 'these are the sample phrases!!!!! ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment