如何告诉 JSLint/JSHint 已经定义了哪些全局变量

在我的项目中,我们有一些作为容器的全局变量:

MyProject.MyFreature.someFunction = function() { ... }

然后我在整个站点上使用这个脚本,JSLint/JSHint 抱怨说:

“ MyProject”没有定义

我知道我可以访问每个 JavaScript 文件并在其上添加注释 /*global MyProject*/。但是我正在寻找一种方法来定义这个注释在某种配置文件中,这样我就不必逐个文件添加这个注释了。

config/jshint.yml的某些类型的期权将是不错的。

44282 次浏览

JSLint has a textarea below the options that says predefine global variables here in it. Just add the variable names in there before running the check.

JSHint doesn't allow you to add global variables, but you can uncheck the When variable is undefined option to suppress that warning.

The JSHint library also has parameters for globals, if you run it as a library . . . details in here: http://jshint.com/docs/

For JSHint you can create .jshintrc to your project directory with

{
"globals": { "MyProject": true }
}

This is only for globals

/* global MyProject */

In your case you need

/* exported MyProject */