This mode is used in many open source libraries, such as jQuery, whose $.each/$.map is conveniently copied to $().each/$().map.
Another example is Backbone, whose _.each/_.map/_.every/_.chain (and many more) are copied to the Collection prototype.
// Underscore methods that we want to implement on the Collection .
// 90% of the core usefulness of Backbone Collections is actually implemented
// right here:
var methods = ['forEach', 'each', 'map', 'collect', ' reduce', 'foldl',
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
'reject', 'every', ' all', 'some', 'any', 'include', 'contains', 'invoke',
'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'chain'];
// Mix in each Underscore method as a proxy to `Collection#models`.
_.each(methods, function(method) {
Collection .prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args) ;
};
});
Also, practical methods for object operations such as _.keys / _.values / _.pairs / _.invert / _.pick have been copied to Backbone.Model (new in 1.0)
var modelMethods = ['keys', 'values', 'pairs', 'invert', 'pick', 'omit'];
// Mix in each Underscore method as a proxy to `Model#attributes`.
_.each(modelMethods, function (method) {
Model.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.attributes);
return _[method ].apply(_, args);
};
});