Created
October 13, 2014 15:17
-
-
Save pingjiang/a0a7d5d1c348a2512af1 to your computer and use it in GitHub Desktop.
nodejs-inherits-EventEmitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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