JSLint message: Unused variables

大憨熊 提交于 2019-11-27 11:25:16

问题


what can I do if JSLint complains about "i" being an unused variable in such a 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'm only using "item".

Is there any other solution than tolerating unused variables or rewriting the $.each to use the index, both solutions which I would prefer not to do?

Thanks in advance.

Update: I appreciate all the suggestions but this code is simply an example to show you what I mean and I'm interested to see a general solution, if there's any. Thanks.


回答1:


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



回答2:


I think this must be new in: http://www.jslint.com/help.html

"JSLint introduces a new reserved word: ignore"

So the above simply becomes:

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

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


Earlier (wrong) answer:

To placate both JsLint and browsers I needed to use:

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

The browser crashed on "undefined(d)" due to undefined not being a function. So the "undefined !== win.undefined" skips the line if we are in a browser.




回答3:


you could do this:

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

...but that's probably worse if you ask me.




回答4:


A possible way to get rid of the warning in a way that is fairly self-documenting is to cause the unused variable to get used, like this:

// 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 the situation where you mark a variable as unused, and you still use it somewhere. Also, this method might be too verbose, depending on the context.

This method has the advantage that it is precise. Using /*jslint unparam*/ might be too broad.




回答5:


How about using void to make explicit that you are intentionally not using the variable?

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

This is also useful in abstract functions that are expected to be overwritten, but where you want to show the signature, or in mocks, where you are ignoring arguments, but want to match the mocked function's signature.




回答6:


I rename "i" as "unused". It still leaves the error obviously, but I see it in the list and know I've "checked" that error and am okay with it.




回答7:


In this particular case of 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('');



回答8:


If function has more than one unused parameters, you can use "ignore" like this :

function (ignoreFoo, ignoreBar, baz) {
}

It must simply begin with the reserved word "ignore" (ignore, ignoreFoo, ignoreBar, ...).



来源:https://stackoverflow.com/questions/6583623/jslint-message-unused-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!