Skip to content

Instantly share code, notes, and snippets.

@brendanashworth
Last active August 29, 2015 14:07
Show Gist options
  • Save brendanashworth/c02e51d4c756c4355480 to your computer and use it in GitHub Desktop.
Save brendanashworth/c02e51d4c756c4355480 to your computer and use it in GitHub Desktop.
Example for the Node.js Event Emitter http://blog.ashworth.in/how-to-use-the-node-js-eventemitter/
var util = require('util');
var events = require('events');
// Create the object constructor
function Cat(name) {
this.name = name;
}
// Inherit the EventEmitter
util.inherits(Cat, events.EventEmitter);
// Make the cat meow with a message
Cat.prototype.meow = function(message) {
this.emit('meow', message);
}
// Create a cat
var kitty = new Cat('Mittens');
kitty.on('meow', function(msg) {
console.log('We got a message from Mittens:', msg);
});
kitty.meow('Purrrr');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment