在 JSLint 中抑制未使用的函数警告

Suppress unused function warning in JSLint

我在更新网页进度条的较大脚本中有一个 JS 函数:

function Funktion(t) {
  'use strict';
  var value,
    max,
    go;
  go = setInterval(animate, t);
  value = 0;
  max = 100;
  go = 0;
  function animate() {
    if (value >= max) {
      clearInterval(go);
      return;
    }
    value += 1;
    document.getElementById('progress').value = value;
    document.getElementById('percent').innerHTML = value;
    if (value === max) {
      clearInterval(go);
    }
  }
}

免责声明:请假设所有相关变量都已正确定义和初始化。脚本作为一个整体正常运行。 JSLint 抱怨 Unused 'animate'. function animate() {。我尝试结合使用 /*global go: true *//*global animate */ 来抑制此警告。有没有办法在不侵入 JSLint 规则的情况下抑制此警告?

针对未使用的变量试试这个:

/*jshint unused: true, node: true */


对于作为参数出现的未使用变量尝试这个​​(将这个与上面的一起使用):

/*jslint unparam: true, node: true */

比我最初想象的还要容易。您只需要在使用前定义函数即可。

/*jslint white:true, browser:true */

function Funktion(t) {
  'use strict';
  var value,
    max,
    go;

  function animate() {
    if (value >= max) {
      clearInterval(go);
      return;
    }
    value += 1;
    document.getElementById('progress').value = value;
    document.getElementById('percent').innerHTML = value;
    if (value === max) {
      clearInterval(go);
    }
  }

  go = setInterval(animate, t);
  value = 0;
  max = 100;
  go = 0;
}

JSLint 非常注重在使用前定义事物。例如,如果您调用您稍后在文件中定义的函数,它会报错。

所以你在 JSLint 知道它是什么之前使用了 animate。它的评估非常程序化。

所以您最初使用的是未定义的 animate,JSLint 可能应该对此大喊大叫,而不是允许您使用 functionvar 周围结束 运行 声明稍后声明。

然后你 "re-defined" animate (来自 JSLint 的 pov)作为一个函数,并且 从未使用过它 。哈哈

如果您尝试 your code (with a few JSLint directives added) at the more recently updated engine at jslint.com:

,您会收到更好的错误消息

'animate' is out of scope.

该错误消息指向 go = setInterval(animate, t); 行,这更有帮助。