Skip to content

Instantly share code, notes, and snippets.

@tamg
Created February 26, 2017 00:07
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 tamg/582e5b052b1def7bac07ffa468bcbd3f to your computer and use it in GitHub Desktop.
Save tamg/582e5b052b1def7bac07ffa468bcbd3f to your computer and use it in GitHub Desktop.
//example by Anthony Alicea
//Inheriting from the Event Emitter using util.inherits
var EventEmitter = require('events');
class Greetr extends EventEmitter {
//any method inside constructor is available directly to any instance object
//super() calls the superconstructor (EventEmitter) we are inheriting from
constructor() {
super();
this.greeting = 'Hello world!';
}
//add a greet method on the prototype
greet(data) {
console.log(this.greeting + data);
this.emit('greet', data);
}
}
//create a new object greeter1
var greeter1 = new Greetr();
//we can now also use emit because Greetr is inheriting from EventEmitter
greeter1.on('greet', function(data) {
console.log('Someone greeted!: ' + data);
});
greeter1.greet('Jane');
//Hello World!: Jane
//Someone greeted!: Jane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment