10

I'm using require.js and one of my modules requires another one, but is not interested in its export (it's a jQuery plugin), i.e. in the code

define(['jquery', 'jquery.mousewheel', 'fabric'], function ($, something, fabric) {
  // ...
}

I'm not interested in something. Of course I can move the dependencies of which results I'm not interested in to the end of the array and just omit the corresponding "trailing" parameters to my function, but for readability I'd like to keep them as shown. Which leads me to the question...

What is the JavaScript idiom for marking ignored parameters in function definition?

Of course I know I can use any name which discourages me from using the variable:

someMethodWithCallback(1, 2, 3, function (dx, notinterested1, notinterested2, point) {
  // ...
});

and was quite surprised that the following works (in Chrome at least) (because undefined is not a keyword):

someMethodWithCallback(1, 2, 3, function (dx, undefined, undefined, point) {
  // ...
});

but of course changing undefined value inside the function is potentially very error-prone...

To ignore parameter value Perl has undef, StandardML has _; has JavaScript anything like that, at least on "folklore" level?

Community
  • 1
  • 1
pwes
  • 2,040
  • 21
  • 30
  • You can use the `shim` option to load jQuery plugins as well. http://stackoverflow.com/a/10813415/1331430 – Fabrício Matté Sep 08 '13 at 00:17
  • I know, I do. However, I'm not interested in their export. – pwes Sep 08 '13 at 00:22
  • `shim: { 'jquery.mousewheel': ['jquery'] }` won't create any export I believe. But back on to the main question, I'm not aware of any semantics for that. I'd just name the parameter properly and not use it. That at least makes it clear why the parameter is there in the first place. – Fabrício Matté Sep 08 '13 at 00:24
  • Right, the shim won't create the export. But I have to put a 'placeholder' parameter for the export in the function anyway (which will be assigned undefined value). – pwes Sep 08 '13 at 00:30
  • If you know the parameter will never be passed undefined or null, you can do this: `someMethodWithCallback({}, {}, {}, function ...`. Mind you, modern JS engines will generate an empty pattern warning, which is why this is a comment rather than an answer. – Marcelo Cantos Nov 07 '17 at 20:30

1 Answers1

10

The convention our team follows is the Erlang's one: prepend names of these variables with _. Actually, it's quite often used in jQuery callbacks:

$.each(someArray, function(_i, el) { ... });

This way you can still see the meaning of the ignored param, but at the same time understand that it's ignored indeed.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • 1
    I tried this approach and I like it. Especially when combined with the same name for all ignored names, like: function (_ign, _ign, _ign, important_param) {...} – pwes Sep 10 '13 at 10:49
  • 4
    Note that using duplicate formal arguments will throw SyntaxError in the strict mode. – raina77ow Sep 10 '13 at 10:55
  • Is it legal to not specifically name the variable. i.e. just use the single character `_`? – JHBonarius Sep 16 '21 at 13:12
  • 1
    @JHBonarius `_` is perfectly valid identifier, so yes – pwes Mar 11 '22 at 09:54