Say I have a function that takes an arbitrary number of arguments (the last is callback):
xxx.create ('arg1', 'arg2', ..., 'arg10', callback) {
...
}
But it’s ugly. I want to be able to extract the first few parameters and do something like:
var args = ['arg1', 'arg2', ..., 'arg10']
xxx.create (args.explode(), callback) {
...
}
Of course I can write a wrapper for xxx.create(), but I want it to be clean.
Thanks.
You’re looking for
Function.apply.I used
Array.concatso as to not mutate the originalargsarray. If that’s not a problem, either of these will suffice:Now, if you wanted to use a wrapper instead of exposing the
Function.applycall:will do the job.