Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Created November 6, 2014 14:04
Show Gist options
  • Save james-gardner/adcbd28f5035b263b04b to your computer and use it in GitHub Desktop.
Save james-gardner/adcbd28f5035b263b04b to your computer and use it in GitHub Desktop.
Cherry pick and pre-bind underscore methods (borrowed from backbone)
var Url = function (url) {
this._url = url;
};
_.extend(Url.prototype, {
/* more prescriptive methods */
});
var methods = ['forEach', 'each', 'map', 'find', 'detect', 'filter',
'select', 'reject', 'every', 'all', 'some', 'any', 'include',
'contains', 'invoke', 'toArray', 'first', 'initial', 'rest',
'last', 'without', 'isEmpty', 'pluck'];
_.each(methods, function(method) {
Url.prototype[method] = function() {
var fragments = this._url.split('/');
var args = [fragments].concat(_.toArray(arguments));
return _[method].apply(_, args);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment