Skip to content

Instantly share code, notes, and snippets.

@pingjiang
Created October 13, 2014 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingjiang/a0a7d5d1c348a2512af1 to your computer and use it in GitHub Desktop.
Save pingjiang/a0a7d5d1c348a2512af1 to your computer and use it in GitHub Desktop.
nodejs-inherits-EventEmitter
var util = require("util");
var events = require("events");
function MyStream() {
events.EventEmitter.call(this);
}
util.inherits(MyStream, events.EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
var stream = new MyStream();
console.log(stream instanceof events.EventEmitter); // true
console.log(MyStream.super_ === events.EventEmitter); // true
stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment