Created
August 19, 2019 06:16
-
-
Save MelodicCrypter/1cab1634ef3db1a2c840f0e9c7003cdf to your computer and use it in GitHub Desktop.
NodeJS advanced EventEmitter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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