27

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?

user247702
  • 23,641
  • 15
  • 110
  • 157
  • I think the better solution is provided in [Skipping optional function parameters in JavaScript][1] [1]: http://stackoverflow.com/questions/8356227/skipping-optional-function-parameters-in-javascript/8356945 – Mohsenme Aug 06 '13 at 22:41
  • 1
    @Mohsenme That's a different question, it's about passing parameters to functions. This one is about defining a callback function. – user247702 Aug 07 '13 at 09:12

4 Answers4

15

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.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 4
    Yeah, I'd avoid using underscores if you're planning on using underscore.js or lodash. I just got bit by this myself so better to just use other placeholders. – Dave Feb 23 '16 at 15:09
12

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.

Lukas
  • 9,752
  • 15
  • 76
  • 120
  • 1
    That syntax wasn't available back in 2012, but it's great that you posted this answer. Marking as accepted so future readers don't accidentally use an outdated practice :) – user247702 Jul 16 '19 at 20:41
3

You're using two arguments which have the same name. You should write so :

function (_, __, myXhr)
  • I'm not using the values of those parameters, so even though one will be overwritten by the other, it doesn't matter if they have the same name. – user247702 Mar 27 '12 at 11:33
  • 3
    ok, so you're using the right method! The _ character is often used to skip parameters or to create partial functions in several languages. – Rodolphe BELOUIN Mar 27 '12 at 11:36
1

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 */ })
Dave Meehan
  • 3,133
  • 1
  • 17
  • 24