Created
August 2, 2012 22:49
-
-
Save FireyFly/3241604 to your computer and use it in GitHub Desktop.
Append extra arguments to a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function a(a1, callback) { | |
// ...does stuff | |
callback("a", "b", "c") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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