Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created May 11, 2013 17:56
Show Gist options
  • Select an option

  • Save mheadd/5560811 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/5560811 to your computer and use it in GitHub Desktop.
Fun with event emitters.
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