The following node.js script is not working
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var TickE = function() {
}
util.inherits(TickE, EventEmitter); //TickE.prototype.__proto__ = EventEmitter.prototype;
TickE.prototype.ticker = function() {
var self = this;
setInterval (function () {
self.emit('tick');
}, 1000);
};
var t = new TickE ();
//console.log (util.inspect(t));
t.on('tick', function() { console.log ('Tick...');});
t.ticker();
It is not working if I call the emit method like below
TickE.prototype.ticker = function() {
//var self = this; // commented this line
setInterval (function () {
this.emit('tick'); // using this in place of self
}, 1000);
};
self is just a variable holding reference of this and why this is throwing error ?