Skip to content

Instantly share code, notes, and snippets.

@bvandgrift
Created March 31, 2016 18:46
Show Gist options
  • Save bvandgrift/0ec92097c8dee0ef002bbb8c6237be2a to your computer and use it in GitHub Desktop.
Save bvandgrift/0ec92097c8dee0ef002bbb8c6237be2a to your computer and use it in GitHub Desktop.
fiddling around with the `arguments` construct in .js
// takes a fn and a number of arguments to append to the
// fn's argument list
var rPartial = function(fn, extras) {
var postArgs = [].slice.call(arguments).slice(1);
return function(vars) {
var preArgs = [].slice.call(arguments);
var allArgs = preArgs.concat(postArgs);
return fn.call(fn.this, ...allArgs);
};
}
// takes a fn and a number of arguments to prepend to
// the fn's argument list
var lPartial = function(fn, extras) {
var preArgs = [].slice.call(arguments).slice(1);
return function(vars) {
var postArgs = [].slice.call(arguments);
var allArgs = preArgs.concat(postArgs);
return fn.call(fn.this, ...allArgs);
};
}
// returns the argument list, for demonstration
var flog = function(args) {
return [].slice.call(arguments);
}
var ary = [0,1,2,3,4];
var rflog = rPartial(flog, 5, 6);
var lflog = lPartial(flog, 5, 6);
console.log(lflog(...ary));
console.log(rflog(...ary));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment