'Skipping parameters in callback function
The callback function I'm working with has the following signature (from http://api.jquery.com/load/):
complete(responseText, textStatus, XMLHttpRequest)
Now, I only need the third parameter. In Lua there's a convention where an underscore is used to skip unneeded return values from functions (skip because _ will actually hold the value):
var1, _, _, var4 = func()
So I thought of doing a similar thing with JavaScript and set my function signature to this:
function (_, _, XMLHttpRequest)
Is there anything wrong with this approach, perhaps there's a better/cleaner way?
Solution 1:[1]
I acknowledge that using _
is a common pattern to omit parameters that prepend the one you want. That's cool for one parameter, maybe 2.
somethingWithACallback((_, whatIAmLookingFor) => {
// ...
})
but I got stuck needing the 5th one. This would mead I'd have to write
somethingWithACallback((_, __, ___, ____, whatIAmLookingFor) => {
// ...
})
For that case I propose this pattern:
somethingWithACallback((...args) => {
const whatIAmLookingFor = args[4];
})
With destructuring, you can also do this
somethingWithACallback((...args) => {
const [,,,,whatIAmLookingFor] = args;
})
and apply that to multiple parameters
somethingWithACallback((...args) => {
const [,,,,whatIAmLookingFor,,andAnotherThing] = args;
})
and thereby essentially pick what you need.
Solution 2:[2]
The technique is not pretty, but I use it myself on several occasions. I guess it is still quite better to give those unused arguments meaningful names (just to avoid confusion), but you're fine in using underscores.
I often see it used in jQuery related callbacks, where the index
is often passed in as first argument, like
$('.foo').each(function(_, node) {
});
because most of the time, you don't care about the index there. So to answer your actual question, there is nothing wrong in using the technique (beside confusion maybe) and there is no better/cleaner way to skip unwanted arguments.
Solution 3:[3]
You're using two arguments which have the same name. You should write so :
function (_, __, myXhr)
Solution 4:[4]
You can combine the variadic definition with array destructuring.
something((...[,,req]) => { /* req.blah */ }
If you are using Typescript, you will need to add type definitions for the arguments, and you can either be generic:
something((...[,,req]: any[]) => { /* req.blah */ })
Or specific:
something((...[,,req]: [ResponseText, TextStatus, XMLHttpRequest]) => { /* req.blah */ })
Or, as you are discarding the non-required parameters, you could use any
(or unknown
) for those:
something((...[,,req]: [any, any, XMLHttpRequest]) => { /* req.blah */ })
If you find that verbose, then introduce a type:
type Params = [ResponseText, TextStatus, XMLHttpRequest]
something((...[,,req]: Params) => { /* req.blah */ })
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lukas |
Solution 2 | jAndy |
Solution 3 | Rodolphe BELOUIN |
Solution 4 | Dave Meehan |