JSLint Message: Unused Variables

what can I do if JSLint complains that "i" is an unused variable in this scenario:

var items = "<option selected></option>"; $.each(data, function (i, item) { items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); 

(i, item) is the required order of parameters, and I use only "item".

Is there any other solution than transferring unused variables or rewriting $ .each to use an index, both solutions that I would rather not do?

Thanks in advance.

Update: I appreciate all the suggestions, but this code is just an example to show you what I mean, and I am interested to see a general solution, if any. Thank.

+76
javascript jquery jslint
Jul 05 2018-11-11T00:
source share
8 answers

Try:

 var items = "<option selected></option>"; /*jslint unparam: true*/ $.each(data, function (i, item) { items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); /*jslint unparam: false*/ // so that you still get warnings from other functions 
+76
Jul 05 2018-11-11T00:
source share
β€” -

I think this should be new at: http://www.jslint.com/help.html

"JSLint introduces a new reserved word: ignore"

So this is just being done:

 $.each(data, function (ignore, item) { 

i => ignore ... too easy. The rest of the code can stay the same, browsers are happy, and JSLint is happy




Previously (incorrect) answer:

To reassure both JsLint and browsers, I had to use:

 function (d, i) { if (undefined !== win.undefined) { undefined(d); } return (i); } 

The browser crashed into "undefined (d)" due to undefined that is not a function. Thus, "undefined! == win.undefined" skips the line if we are in the browser.

+23
Jul 08 '15 at 14:38
source share

You can do it:

 var items = "<option selected></option>"; $.each(data, function () { var item = arguments[1]; items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); 

... but this is probably worse if you ask me.

+1
Jul 05 2018-11-11T00:
source share

A possible way to get rid of the warning in a way that is fairly self-documenting is to force the unused variable to be used, for example:

 // Utility function in project scope: function unusedVariables(/* Put all your deliberately unused variables here */) { // pass } // And then, later: var items = "<option selected></option>"; $.each(data, function (i, item) { unusedVariables(i); //< This is the new and magical line items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); 

Of course, now you can get into a situation where you mark a variable as unused and you are still using it somewhere. In addition, this method may be too detailed, depending on the context.

This method has the advantage of being accurate. Using /*jslint unparam*/ might be too wide.

+1
Jul 05 2018-11-15T00:
source share

How to use void to make explicit that you are not intentionally using a variable?

 $.each(data, function (i, item, any, other, unused, vars) { void(i, any, other, unused, vars); items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); 

It is also useful in abstract functions, which are expected to be overwritten, but where you want to show the signature or in mocks, where you ignore the arguments, but want to match the forged function signature.

+1
Sep 13 '13 at 23:59 on
source share

I renamed "i" as "unused". It still leaves an error, but I see it on the list and I know that I β€œchecked” this error and everything is in order with it.

+1
Nov 14 '13 at 6:38
source share

In this particular case, transforming an array / object http://api.jquery.com/jquery.map/ (or http://api.jquery.com/map/ ?) Is an option.

 var items = "<option selected></option>" + $.map(data, function (item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; }).get().join(''); 
0
Jan 30 '17 at 7:10
source share

If a function has several unused parameters, you can use ignore as follows:

 function (ignoreFoo, ignoreBar, baz) { } 

It should just start with the reserved word "ignore" (ignore, ignoreFoo, ignoreBar, ...).

0
Apr 25 '17 at 21:08
source share



All Articles