Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created May 13, 2014 14:23
Show Gist options
  • Save badsyntax/a8f498a2a0469ffadc39 to your computer and use it in GitHub Desktop.
Save badsyntax/a8f498a2a0469ffadc39 to your computer and use it in GitHub Desktop.
var EventEmitter = require('events').EventEmitter;
function Clock() {
var self = this;
this.getTime = function() {
console.log("In getTime()");
setInterval(function() {
self.emit('time', self.getTime);
console.log("Event Emitted!!");
}, 1000);
};
}
Clock.prototype = EventEmitter.prototype;
Clock.prototype.constructor = Clock;
Clock.uber = EventEmitter.prototype;
function fromMethod(method, callback) {
return function() {
var args = Array.prototype.slice.call(arguments);
var sourceMethod = args.pop();
if (sourceMethod instanceof Function && sourceMethod === method) {
callback.apply(null, args);
}
}
}
var timer = new Clock();
timer.on("time", fromMethod(timer.getTime, function() {
console.log(new Date());
}));
timer.getTime();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment