简体   繁体   English

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

[英]How to tell JSLint / JSHint what global variables are already defined

In my project we have some global variables that work as containers: 在我的项目中,我们有一些全局变量作为容器:

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

So then I use that script across the site and JSLint / JSHint complains about that: 那么我在整个网站上使用该脚本,JSLint / JSHint抱怨:

'MyProject' is not defined “MyProject”未定义

I know that I can go to every JavaScript file and add the comment /*global MyProject*/ on top of it. 我知道我可以访问每个JavaScript文件并在其上添加注释/*global MyProject*/ But I'm looking a way to define that comment in some sort of config file so I don't have to go file by file adding this comment. 但我正在寻找一种方法来在某种配置文件中定义该注释,所以我不必逐个文件添加此注释。

Some kind on option in the config/jshint.yml would be nice. config/jshint.yml中的某种选项会很好。

For JSHint you can create .jshintrc to your project directory with 对于.jshintrc ,您可以使用创建.jshintrc到项目目录

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

This is only for globals 这仅适用于全局变量

/* global MyProject */

In your case you need 在你的情况下,你需要

/* exported MyProject */

JSLint has a textarea below the options that says predefine global variables here in it. JSLint在选项下面有一个textarea,它在其中predefine global variables here 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. JSHint不允许您添加全局变量,但您可以取消选中When variable is undefined选项来禁止该警告。

The JSHint library also has parameters for globals, if you run it as a library . 如果将JSHint库作为库运行,它还具有全局变量的参数。 . . details in here: http://jshint.com/docs/ 详情请访问: http//jshint.com/docs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM