Skip to content

Instantly share code, notes, and snippets.

@FireyFly
Created August 2, 2012 22:49
Show Gist options
  • Save FireyFly/3241604 to your computer and use it in GitHub Desktop.
Save FireyFly/3241604 to your computer and use it in GitHub Desktop.
Append extra arguments to a function
function a(a1, callback) {
// ...does stuff
callback("a", "b", "c")
}
// Returns a function that, when called, calls `fun` with first the arguments passed
// to the returned function, and then any additional bound arguments appended *after*
// that... Sort-of like a reverse `Function.prototypee.bind`.
function appendArgsTo(fun/*, ...extraArgs*/) {
var extraArgs = Array.prototype.slice.call(arguments, 1)
return function(/*...args*/) {
var args = Array.prototype.slice.call(arguments)
return fun.apply(this, args.concat(extraArgs))
}
}
// ...and then invoke it
a("something", appendArgsTo(callback, "D!!!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment