Following the pattern recommended in this question, where we have something akin to:
function foo(a, b, opts) {
}
foo(1, 2, {"method":"add"});
foo(3, 4, {"test":"equals", "bar":"tree"});
How would one then include a callback as the final parameter? I have function foo()
that should be able to handle both of:
foo(x, y, function() {
// do stuff
});
And
foo(x, y, z, function() {
// do stuff
});
Any suggestions?
So basically you want to accept a variable number of arguments, followed by a callback as the last one? Something similar to how PHP's
array_udiff
works?This is fairly simple:
Ended up going with the following solution:
And then simply checking to see if the last arguments value is a function, ala @PaulP.R.O.'s solution above:
I'd say just use the arguments object:
You could do similar to
.apply
. (unlike.call
)And if you need to, you could put them into variables using
.shift
: