Created
May 11, 2013 17:56
-
-
Save mheadd/5560811 to your computer and use it in GitHub Desktop.
Fun with event emitters.
This file contains hidden or 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 EventEmitter = require('events').EventEmitter; | |
| // Create new class. | |
| myCoolObject = function() {}; | |
| myCoolObject.prototype = new EventEmitter; | |
| myCoolObject.prototype.doEventEmmission = function(text) { | |
| var self = this; | |
| self.emit('myCoolEvent', 'This is a cool event: ' + text); | |
| }; | |
| // Create object instance. | |
| var coolObject = new myCoolObject(); | |
| // Register event listener. | |
| coolObject.addListener('myCoolEvent', function(stuff){ | |
| console.log(stuff); | |
| }); | |
| // Emit event. | |
| coolObject.doEventEmmission(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment