简体   繁体   English

JSLint 消息:未使用的变量

[英]JSLint message: Unused variables

what can I do if JSLint complains about "i" being an unused variable in such a scenario:如果 JSLint 在这种情况下抱怨“i”是未使用的变量,我该怎么办:

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". (i, item) 是参数的必需顺序,我只使用“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?除了容忍未使用的变量或重写 $.each 以使用索引之外,还有其他解决方案吗?这两种解决方案都是我不想做的?

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.谢谢。

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

I think this must be new in: http://www.jslint.com/help.html我认为这一定是新的: http://www.jslint.com/help.html

"JSLint introduces a new reserved word: ignore" “JSLint 引入了一个新的保留字:ignore”

So the above simply becomes:所以上面简单地变成了:

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

i => ignore... too easy. i => 忽略...太容易了。 The rest of the code can remain the same, browsers are happy and JSLint is happy代码的rest可以保持不变,浏览器开心,JSLint开心


Earlier (wrong) answer:较早的(错误)答案:

To placate both JsLint and browsers I needed to use:为了安抚我需要使用的 JsLint 和浏览器:

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

The browser crashed on "undefined(d)" due to undefined not being a function.由于 undefined 不是 function,浏览器在“undefined(d)”上崩溃。 So the "undefined.== win.undefined" skips the line if we are in a browser.因此,如果我们在浏览器中,“undefined.== win.undefined”会跳过该行。

How about using void to make explicit that you are intentionally not using the variable?使用void来明确表示您故意不使用该变量怎么样?

$.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.这在预期会被覆盖但要显示签名的抽象函数中也很有用,或者在您忽略 arguments 但想要匹配模拟函数的签名的模拟中也很有用。

I rename "i" as "unused".我将“i”重命名为“未使用”。 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.它仍然明显留下错误,但我在列表中看到它并且知道我已经“检查”了该错误并且可以接受。

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. ...但如果你问我,那可能会更糟。

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.使用/*jslint unparam*/可能过于宽泛。

In this particular case of transforming an array/object http://api.jquery.com/jquery.map/ (or http://api.jquery.com/map/ ?) is an option. 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('');

If function has more than one unused parameters, you can use "ignore" like this:如果 function 有多个未使用的参数,您可以像这样使用“忽略”

function (ignoreFoo, ignoreBar, baz) {
}

It must simply begin with the reserved word "ignore" (ignore, ignoreFoo, ignoreBar, ...).它必须简单地以保留字“ignore”开头(ignore、ignoreFoo、ignoreBar、...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM