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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17709657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:17:38  来源:igfitidea点击:

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

javascriptglobal-variablesjslintjshint

提问by Emiliano Zilocchi

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

未定义“我的项目”

I know that I can go to every JavaScript file and add the comment /*global MyProject*/on top of it. 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.

我知道我可以转到每个 JavaScript 文件并在其/*global MyProject*/上添加注释。但是我正在寻找一种在某种配置文件中定义该注释的方法,因此我不必逐个文件地添加此注释。

Some kind on option in the config/jshint.ymlwould be nice.

中的某种选项config/jshint.yml会很好。

回答by Epeli

For JSHint you can create .jshintrcto your project directory with

对于 JSHint,您可以创建.jshintrc到您的项目目录

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

回答by zevero

This is only for globals

这仅适用于全局变量

/* global MyProject */

In your case you need

在你的情况下,你需要

/* exported MyProject */

回答by talemyn

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

JSLint 在其中显示的选项下方有一个文本区域predefine global variables here。在运行检查之前,只需在其中添加变量名称。

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

JSHint 不允许您添加全局变量,但您可以取消选中该When variable is undefined选项以抑制该警告。

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

JSHint 库还具有全局参数,如果您将其作为库运行。. . 详情在这里:http: //jshint.com/docs/