javascript JSHint 沉默“变量已定义但从未使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22037197/
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-10-27 22:15:21  来源:igfitidea点击:

JSHint silence "Variable is defined but never used"

javascriptjshint

提问by Amal Antony

I want to silence the JSHint warning "attrs is defined but never used"for the variable attrs. However I do not want to use the option /* jshint unused:false */since this will turn off the warning altogether. I want the warning to be disabled only for attrs.

我想消除变量的 JSHint 警告“已定义属性但从未使用过”attrs。但是我不想使用该选项,/* jshint unused:false */因为这将完全关闭警告。我希望仅针对attrs.

回答by Josh Harrison

For global variables

对于全局变量

Add

添加

/* exported variableNameHere */

at the top of your script. In your case, replace variableNameHerewith attrs. This tells jshint that attrswill be used elsewhere.

在脚本的顶部。在您的情况下,替换variableNameHereattrs. 这告诉 jshintattrs将在其他地方使用。

For multiple variables:

对于多个变量:

/* exported attrs, somethingElse, somethingElse2 */

Docs here.

文档在这里

For local variables

对于局部变量

You can ignore allunused local variables within a given function scope using the method outlined in this jshint commitand this GitHub issue. Example:

您可以使用此 jshint 提交和此GitHub 问题中概述的方法忽略给定函数范围内所有未使用的局部变量。例子:

//jshint unused:true
var a;

function foo(b) {
    //jshint unused:false
    return 1;
}

foo();

// ->
// Line 1: 'a' is defined but never used.

This doesn't seem to be documented anywhere else, but works when tested on http://jshint.com/

这似乎没有在其他任何地方记录,但在http://jshint.com/ 上测试时有效