JSHint抱怨对象未定义,但它在外部文件中已定义

3

我在文件remover.js中有以下对象:

'use strict';

(function( remover, $, undefined ) {
  // ...
  // Object's definition
  // ...
}( window.remover = window.remover || {}, jQuery ));

这是在外部文件main.js中使用的:

'use strict';

remover.doSomething();

代码正常运行,但JSHint出现以下问题:
Running "jshint:all" (jshint) task

app/scripts/stuff/main.js
  line 3  col 1  'remover' is not defined.

✖ 1 problem

如何消除此警告?
1个回答

10

你有两个选择:一个是消除所有未定义警告,这对你的调试不利;另一个是具体解决全局实体的问题。

  1. 禁用var未定义警告 (不好!)

    只需使用undef选项在main.js文件上禁用警告即可。

    'use strict';
    /*jshint undef:false */
    
  2. 告诉JSHint哪些是外部库(很好!)

    将每个变量都标记为全局的

  3. 'use strict';
    /*global remover */
    
    现在运行grunt jshint应该输出:

Running "jshint:all" (jshint) task

✔ No problems

JSHint文档指令部分有更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接