Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Created January 3, 2013 00:28
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 ralphtheninja/4439706 to your computer and use it in GitHub Desktop.
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.
// 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))
}
}
@FesterCluck
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment