Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best mark parameter as ignored

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?

like image 678
pwes Avatar asked Sep 08 '13 00:09

pwes


1 Answers

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.

like image 94
raina77ow Avatar answered Sep 21 '22 09:09

raina77ow