Skip to content

Instantly share code, notes, and snippets.

@NV
Created January 3, 2010 12:52
Show Gist options
  • Select an option

  • Save NV/267965 to your computer and use it in GitHub Desktop.

Select an option

Save NV/267965 to your computer and use it in GitHub Desktop.
call() and apply() on steroids
Function.prototype.callMany = function (context) {
var result = [];
for (var i=1; i<arguments.length; i++) {
result.push( this.call(context, arguments[i]) );
}
return result;
}
function hi (name) {
return 'Hi '+ name + '!';
}
hi.callMany(this, 'John', 'Dean') // ['Hi John!', 'Hi Dean!']
Function.prototype.applyMany = function (context) {
var result = [];
for (var i=1; i<arguments.length; i++) {
result.push( this.apply(context, arguments[i]) );
}
return result;
}
function plus (x, y) {
return x + y;
}
plus.applyMany(this, [1, 1], [2, 3]) // [2, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment