Skip to content

Instantly share code, notes, and snippets.

@isao
Last active August 29, 2015 13:58
Show Gist options
  • Save isao/10423591 to your computer and use it in GitHub Desktop.
Save isao/10423591 to your computer and use it in GitHub Desktop.
call a function with a subset (slice) of the arguments passed to it.
function callSliced(fn, a, b) {
var args = [a]; // 1st argument to slice
if (b) args.push(b); // 2nd argument to slice, if any
return function apply() {
return fn.apply(fn, Array.prototype.slice.apply(arguments, args));
};
}
// the function created by `callSliced(console.log, 0, 1)`
// only uses the first argument passed to it
> [1,2,3].forEach(callSliced(console.log, 0, 1));
1
2
3
> [1,2,3].forEach(console.log)
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment