Javascript JSLint 消息:未使用的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6583623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:22:46  来源:igfitidea点击:

JSLint message: Unused variables

javascriptjqueryjslint

提问by TheFitGeekGirl

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.

更新:我感谢所有的建议,但这段代码只是一个例子,向你展示我的意思,我很想看到一个通用的解决方案,如果有的话。谢谢。

采纳答案by nickf

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

回答by PuZZleDucK

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. The rest of the code can remain the same, browsers are happy and JSLint is happy

我 => 忽略...太容易了。剩下的代码可以保持不变,浏览器开心,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);
}
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.

由于 undefined 不是函数,浏览器在“undefined(d)”上崩溃。因此,如果我们在浏览器中,“undefined !== win.undefined” 会跳过该行。

回答by nickf

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.

……但如果你问我,那可能更糟。

回答by Magnus Hoff

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*/可能太广泛了。

回答by xn.

How about using voidto 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.

这在预期会被覆盖的抽象函数中也很有用,但您想要显示签名,或者在模拟中,您忽略参数但想要匹配模拟函数的签名。

回答by DharmaTurtle

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.

我将“i”重命名为“未使用”。它仍然明显地留下了错误,但我在列表中看到了它,并且知道我已经“检查”了那个错误并且对此没有意见。

回答by Greg Domjan

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

在这种转换数组/对象的特殊情况下,http://api.jquery.com/jquery.map/(或http://api.jquery.com/map/?)是一个选项。

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

回答by Pilipe

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, ...).

它必须以保留字“ignore”开头(ignore、ignoreFoo、ignoreBar、...)。