3

I have been wondering what the best method is to deal with the following.

someCallback: function(param1,param2,paramThatIActuallyNeed) {
     doSomethingWith(paramThatIActuallyNeed)
}

So in this example, param1 and param2 are unused, however I need to place them there in order to access that third parameter. This makes JSLint sad and I'm wondering what the practice is to avoid this? This tends to happen when using libraries with many callback results.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 47
  • 1
  • 4
  • 3
    Best practice is to ignore JSLint, there's no other (good) way to access the latter arguments without specifying the former. – adeneo Oct 15 '14 at 10:47
  • 1
    possible duplicate of [JSLint message: Unused variables](http://stackoverflow.com/questions/6583623/jslint-message-unused-variables) –  Oct 15 '14 at 11:32
  • Surprised adeneo's comment has three upticks. JSLint has a flag specifically for the question asked; see [torazaburo's answer, below](http://stackoverflow.com/a/26381544/1028230). – ruffin Oct 15 '14 at 12:14

4 Answers4

2

That is what unparam is for:

/*jslint unparam: true */

This is another good reason to prefer jshint, which does not engage in such stupidity.

I would definitely advise against using arguments just to appease jslint.

  • Out of curiosity, why would you suggest not using the arguments object? – Enzo Oct 15 '14 at 11:52
  • @DarkLord7854 See comment to your answer, above: Because JSLint will still throw an error, `Use a named parameter.` Using the `unparam` flag is the correct answer. – ruffin Oct 15 '14 at 12:09
  • @DarklLord7854 Wrote that before I realized that JSLint would **still** be unhappy even with `arguments`, but in any case it's just that it seems too much trouble to go to simply to make it happy. –  Oct 15 '14 at 14:00
1

You can use the arguments object, detailed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

Ex:

someCallback: function() {
     doSomethingWith(arguments[2]);
}
Enzo
  • 160
  • 1
  • 8
0

You could always use an object to hold your parameters, then it doesn't matter if any are missing as long as you check for them.

function doThing(args) {
  var age = args.age || 12;
  console.log(age);
  if (args.name) { console.log(args.name); }
};

doThing({
  name: 'Bob',
  age: 12
  occupation: 'spaceman'
});
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 1
    The only reason I don't like this answer is because *it assumes you have control over how the function is being called*. If so, *JSLint's original error is actually valid* -- stop passing in those first two parameters if they're not used. The `unparam` workaround is for when you find yourself stuck in event handler land or a similarly constraining convention, and you can't reasonably stop extra params being passed. – ruffin Oct 15 '14 at 12:17
0

Won't appease jslint(*), but you can utilize destructuring to completely omit unused parameters. Granted it's somewhat awkward syntax, but it is an option:

function func(...[, , paramThatIActuallyNeed]) {
  console.log(paramThatIActuallyNeed)
}

func(1, 2, 3); // Logs: 3

There might be a better syntax but the above does work.

(*) jslint is very conservative in what it allows in destructuring parameters and will throw Expected an identifier and instead saw '['. on the above code.

Wasn't entirely sure whether your question was focused on appeasing jslint in your situation or achieving an end result of not actually having unused variable floating around.

ballenf
  • 894
  • 10
  • 19