Created
January 3, 2013 00:28
-
-
Save ralphtheninja/4439706 to your computer and use it in GitHub Desktop.
Showing difference in scope and clarity between object literal assignment to prototype object rather than direct assignment of methods to method properties.
This file contains 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
// Compare the following. In this snippet it's unclear in what context these methods are defined | |
, pipe: function (dest) { | |
if (typeof dest.add == 'function' && this._options.type == 'fstream') { | |
this._dataEvent = 'entry' | |
this.on('entry', function (data) { | |
var entry = new BufferStream() | |
entry.path = data.key.toString() | |
entry.type = 'File' | |
entry.props = { | |
type: 'File' | |
, path: data.key.toString() | |
} | |
entry.once('data', process.nextTick.bind(null, entry.end.bind(entry))) | |
entry.pause() | |
if (dest.add(entry) === false) { | |
this.pause() | |
} | |
entry.write(data.value) | |
}.bind(this)) | |
} | |
return Stream.prototype.pipe.apply(this, arguments) | |
} | |
, _read: function () { | |
if (this._status == 'ready') { | |
this._status = 'reading' | |
this._iterator.next(this._cleanup.bind(this), this._onData.bind(this)) | |
} | |
} | |
// Here it's immediately clear what methods we are defining. | |
ReadStream.prototype.pipe = function (dest) { | |
if (typeof dest.add == 'function' && this._options.type == 'fstream') { | |
this._dataEvent = 'entry' | |
this.on('entry', function (data) { | |
var entry = new BufferStream() | |
entry.path = data.key.toString() | |
entry.type = 'File' | |
entry.props = { | |
type: 'File' | |
, path: data.key.toString() | |
} | |
entry.once('data', process.nextTick.bind(null, entry.end.bind(entry))) | |
entry.pause() | |
if (dest.add(entry) === false) { | |
this.pause() | |
} | |
entry.write(data.value) | |
}.bind(this)) | |
} | |
return Stream.prototype.pipe.apply(this, arguments) | |
} | |
ReadStream.prototype._read = function () { | |
if (this._status == 'ready') { | |
this._status = 'reading' | |
this._iterator.next(this._cleanup.bind(this), this._onData.bind(this)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You know, I dealt with this very issue today while navigating the jqGrid source. Both are obviously functional, but the second is much more readable, especially when starting from a breakpoint or a text search. My style has been the former in the past, I think I'll promote the later.