【问题标题】:JSHint complaining object is not defined when it is defined in external file [duplicate]JSHint抱怨对象在外部文件中定义时未定义[重复]
【发布时间】:2014-10-11 21:32:20
【问题描述】:

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

'use strict';

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

这用于外部文件ma​​in.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

如何删除此警告?

【问题讨论】:

    标签: javascript jshint


    【解决方案1】:

    您有两种选择,一种是解决所有对您的调试不利的undefined警告,另一种是专门解决全局实体的问题。

    1. 禁用 var undefined 警告 (不好!)

      只需使用 undef 选项禁用 ma​​in.js 文件上的警告

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

      将每个变量称为全局

      'use strict';
      /*global remover */
      

    现在运行grunt jshint 应该会输出:

    Running "jshint:all" (jshint) task
    
    ✔ No problems
    

    JSHint Documentation 中的指令部分提供了更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2014-09-03
      • 2011-12-31
      • 2020-11-01
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多