Last active
August 29, 2015 14:07
-
-
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/
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
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