Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created April 22, 2015 12:00
Function Hoisting, Prototype Chains, Exports, And process.nextTick() In Node.js
// Require our core node modules.
var stream = require( "stream" );
var util = require( "util" );
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// Wait until the end of this event loop to run the demo code. This will give the
// current file a chance to define all of the variables including the prototypes. We
// always get the variables and function declarations hoisted; but, this next-tick
// will allow the .inherits() and .prototype lies to be executed pre-demo.
process.nextTick(
function runDemo() {
// Create an instance of our writable stream and try to write to it.
var myStream = new SomeStream();
myStream.write( "Hello world!" );
myStream.write( "What it be like?!" );
}
);
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// I am a sub-class of Writable stream.
function SomeStream() {
// Call the super constructor.
stream.Writable.call( this );
}
util.inherits( SomeStream, stream.Writable );
// When you write to this stream, all I do is turn around and log it to the console.
SomeStream.prototype._write = function( chunk, encoding, doneWriting ) {
console.log( "Written to stream:", chunk.toString( "utf-8" ) );
doneWriting();
};
// Require our core node modules.
var stream = require( "stream" );
var util = require( "util" );
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// Create an instance of our writable stream and try to write to it.
var myStream = new SomeStream();
// CAUTION: This will ERROR because at this point in time, we were able to call
// the constructor (SomeStream) due to function-hoisting; but, the .inherits() and
// .prototype lines of code have not had a chance to run. As such, SomeStream has not
// had a chance to actually inherit from stream.Writable at this point.
myStream.write( "Hello world!" );
myStream.write( "What it be like?!" );
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// I am a sub-class of Writable stream.
function SomeStream() {
// Call the super constructor.
stream.Writable.call( this );
}
util.inherits( SomeStream, stream.Writable );
// When you write to this stream, all I do is turn around and log it to the console.
SomeStream.prototype._write = function( chunk, encoding, doneWriting ) {
console.log( "Written to stream:", chunk.toString( "utf-8" ) );
doneWriting();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment