3
I was watching the video of a guy explaining events of Node.js and at the end he makes a small module using util.inherit
to inherit from one prototype constructor to another. Although I know the syntax of call
and have caught a sense of the method util.inherit
in API DOCS, I couldn’t capture the semantics of the code in general.
Module builder
var EventEmitter - require('events').EventEmitter;
var inherits = require('util').inherits;
function FireDetector(){
EventEmitter.call(this);
}
inherits(FireDetector, EventEmitter);
FireDetector.prototype.smoke = function(amount){
if(amount > 0.5) {
this.emit('fire').
}
}
module.exports = FireDetector;
Example of module usage (only as bonus)
var FireDetector = require('./firedetector');
fireDetector = new FireDetector();
fireDetector.on('fire', function() {
console.log('fire fire fire');
});