Skip to content

Instantly share code, notes, and snippets.

@zachwolf
Created October 9, 2014 16:39
Show Gist options
  • Save zachwolf/0b54944f03790aaeb873 to your computer and use it in GitHub Desktop.
Save zachwolf/0b54944f03790aaeb873 to your computer and use it in GitHub Desktop.
Make a JavaScript objects methods chainable
/**
* Allows for chaining on an object.
* Functions will be able to be called normally,
* but all other types need to be accessed using
* `Chain.set(prop, val)` and `Chain.get(prop)`
*
* I haven't tested this in many browsers yet.
* Until then, it's probably safer to use something like
* LeaVerou's Chainvas
* http://leaverou.github.io/chainvas/
*
* @param {object} target - object of things to wrap
* @returns {object}
*/
function Chain(target) {
this.target = target
function wrap(value, prop) {
if (_.isFunction(value)) {
this[prop] = _.bind(function(){
this.target[prop].apply(this.target, _.toArray(arguments))
return this
}, this)
}
return
}
_.each(this.target, wrap, this)
_.each(this.target.constructor.prototype, wrap, this)
return this
}
/**
* Changes a property value on the original target
*
* @param {string} prop - name of the property being changed
* @param {*} val - new value
* @returns {object}
*/
Chain.prototype.set = function(prop, val) {
this.target[prop] = val
return this
}
/**
*
* @param {string} prop -
* @returns {*}
*/
Chain.prototype.get = function(prop) {
return this.target[prop]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment