Javascript 防止 JSHint 警告“functionName 已定义但从未使用过”

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

Prevent JSHint warning that 'functionName is defined but never used'

javascriptjqueryjslintjshint

提问by Undistraction

I have just started using JSHint (through the Sublime-Linter package for Sublime Text 2). I would like to suppress its warnings regarding functions that are used before they are defined, as I see no problem with using function definitions like this. For example, the following code generates warnings:

我刚刚开始使用 JSHint(通过 Sublime Text 2 的 Sublime-Linter 包)。我想取消关于在定义之前使用的函数的警告,因为我认为使用这样的函数定义没有问题。例如,以下代码会生成警告:

(function($){ 

    $(document).ready(function()
    {
      formValidationSetup();
      refreshErrorMessages();
    });

    function formValidationSetup()
    {

    }

    function refreshErrorMessages()
    {

    }

})(jQuery);

The warnings:

警告:

  1. formValidationSetup is defined but never used
  2. refreshErrorMessages is defined but never used
  1. formValidationSetup 已定义但从未使用
  2. refreshErrorMessages 已定义但从未使用

I've tried setting undefto falsein the JSHint options, but I'm still getting these errors. Is there another option I should be setting? Form the JSLint docsfor undef:

我已经尝试在 JSHint 选项中将undef设置为false,但我仍然收到这些错误。我应该设置另一个选项吗?形成的JSLint文档民主基金

true if variables and functions need not be declared before used. This is not available in strict mode.

如果变量和函数在使用前不需要声明,则为 true。这在严格模式下不可用。

回答by Roland Puntaier

To avoid the warning

为了避免警告

defined but never used

定义但从未使用

in jslint in your javascript add comments like:

在 jslint 的 javascript 中添加如下注释:

 /*exported formValidationSetup, refreshErrorMessages */

In jshint and jslint you can set the unused option to false:

在 jshint 和 jslint 中,您可以将未使用的选项设置为 false:

 /*jshint unused:false*/

See Options

查看选项

回答by Michael Cole

I had this problem with shouldand expectin Chai tests. I ended up with this pattern:

我有这个问题,shouldexpect在柴测试。我最终得到了这个模式:

'use strict';

var request = require('supertest');
var should = require('chai').should();  // jshint ignore:line
var expect = require('chai').expect;    // jshint ignore:line

process.env.NODE_ENV = 'test';
var appPromise = require('./index');

describe('GET /r_u_up', function() {

  it('respond with 204', function(done) {
    appPromise.then(function(app) {
      request(app)
      .get('/r_u_up')
      .expect(204, done);
    });
  });

});

回答by imal hasaranga perera

You can simply use

你可以简单地使用

"unused": false,

in your .jshintrc

在你的 .jshintrc

回答by xptest18

A better way not touching the Gruntfile.jsin a typical Yoeman setup is to edit the .jshintrcfile (a hidden file in Unix system). And update the content as the following:

Gruntfile.js在典型的 Yoeman 设置中不触及 的更好方法是编辑.jshintrc文件(Unix 系统中的隐藏文件)。并更新内容如下:

{
  "curly": true,
  "eqeqeq": true,
  "immed": true,
  "latedef": true,
  "newcap": true,
  "noarg": true,
  "sub": true,
  "undef": true,
  "unused": false, // the change is here
  "boss": true,
  "eqnull": true,
  "node": true
}

set the "unused"to false.

设置"unused"false.

回答by Undistraction

Interestingly, adding 'use strict';inside the IIFE suppresses the error. Not sure why though.

有趣的是,'use strict';在 IIFE 内部添加可以抑制错误。不知道为什么。

回答by cyberwombat

You'll want to use the 'latedef' option

您需要使用 'latef' 选项