VarName未定义,请修复或添加/ *全局VarName * / Cloud9 [英] VarName is not defined, please fix or add /*global VarName*/ Cloud9
问题描述
目标
阻止Cloud9 IDE向我发出警告消息。
背景
我正在使用Cloud9 IDE编写JavaScript,无论我在哪里使用其他文件中的类(在同一文件夹中),我都会收到警告消息:
未定义VarName,请修复或添加/ * global VarName * /
现在这让我烦恼,我想解决它。
显而易见的解决方案是添加注释 / * global VarName * /
并完成它,但我不相信这是一个好习惯。
我尝试了什么
经过研究,我来了整个JavaScript import
功能。理论上,这将允许我做一些像 importmyOtherJsFile
这样的事情并完成它。
这个对于该问题,这将是一个很好的标准化解决方案,因为许多其他语言采用相同的方法。
问题
然而,即使在添加上述行之后,我仍然会收到警告。它不会消失。
问题
- 我做错了吗?
- 有没有办法在不向我的代码添加评论的情况下删除警告?
- 还有其他方法可以实现我的目标吗?
创建官方论坛上的帖子我现在有足够的信心在这里发布我所学到的知识。
摘要
讨论了四种解决问题的方法:
- 使用项目禁用变量警告
- 使用所有全局变量创建
.eslintrc
文件 - 使用
import
和export
- 在文件顶部添加注释
/ * global varName * /
解决方案1
T. o避免消息可以简单地关闭项目设置中的标记未定义变量选项。
虽然此解决方案可行,但它只会停止标记所有未定义的变量,甚至是你想要它标记的那些。出于这个原因,我不推荐这种方法。
解决方案2
还可以创建 .eslintrc
认为包含所有全局变量的文件,如下所示:
{
globals:{
var1:true,
var2:false
}
}
有关配置的更多信息: http ://eslint.org/docs/user-guide/configuring
这种方法的问题在于每次创建,删除或更改全局变量,您必须更新全局变量文件。
这很快就会变得很麻烦,我甚至不会讨论对大项目的维护影响,这很快就会成为一场噩梦。
我看待它的方式,一种技术性的解决方案,但不实用。
解决方案3
首选解决方案,因为它仅依赖于JavaScript,与其他语言的导入和导出功能非常相似。
然而,这种方法的问题在于,在撰写答案时,没有浏览器支持 export
也不是 import
关键字:
- https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import
- https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export
在将来,这可能会改变,但现在它确实如此,所以我们也不能使用这个解决方案。
解决方案4
通过排除零件,因为解决方案一和二要么容易产生巨大的副作用,要么有巨大的缩放问题,这是我将暂时使用的解决方案。
<它没有任何副作用,由于其独立于其他文件而非常好地扩展,最重要的是,一旦解决方案3可供使用,通过调用
import
keyword。 结论
目前,解决方案4是最好的做法。但是,一旦浏览器添加对解决方案3的支持,就应该使用它来代替解决方案4.
积分
特别感谢Harutyun Amirjanyan坚持不懈,耐心和指导,帮助我解决这个问题。
Objective
Stop Cloud9 IDE from giving me the warning message.
Background
I am coding JavaScript using the Cloud9 IDE, and wherever I use a class from another file (in the same folder) I get the warning message:
VarName is not defined, please fix or add /*global VarName*/
Now this bugs me and I want to fix it.
The obvious solution would be to just add the comment /*global VarName*/
and be done with it, but I don't believe this is a good practice.
What I tried
After researching, I came across the JavaScript import
functionality. In theory, this would allow me to do something like import "myOtherJsFile"
and be done with it.
This would be a good standardized solution for the problem, as many other languages take the same approach.
Problem
However, even after adding the said line, I still get the warning. It wont go away.
Questions
- Am I doing something wrong?
- Is there a way to remove the warning without adding comments to my code?
- Is there another way of achieving my objective?
After creating a thread on the official forums I am now confident enough to post here what I learned.
Summary
There were discussed four ways of solving the problem:
- Disable the variable warnings withing the project
- Creating an
.eslintrc
file with all the globals - Use of
import
andexport
- Adding the comment
/*global varName*/
at the top of the file
Solution 1
To avoid the message one can simply turn off the "mark undefined variables" option in the project settings.
Although this solution will work, it will simply stop marking all undefined variables, even the ones you want it to mark. For this reason, I don't recommend this approach.
Solution 2
One can also create an .eslintrc
think file with all the globals, like the following:
{
"globals": {
"var1": true,
"var2": false
}
}
More information on configuration: http://eslint.org/docs/user-guide/configuring
The problem with this approach is that every time you create, delete or change a global variable, you have to update the global variables file.
This can quickly become cumbersome and I am not even going to discuss the maintenance impacts on big projects, which would soon become a nightmare.
The way I see it, a techy solution, but not practical.
Solution 3
The preferred solution, as it relies solely on JavaScript and is very similar to the import and export functionalities on other languages.
The problem with this approach however, is that at the time of writing of the answer, no browser supports either the export
nor the import
keywords:
- https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import
- https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export
In the future this may change, but for now it is as it is, so we cannot use this solution either.
Solution 4
By exclusion of parts, since solutions one and two are either prone to huge side effects or either have huge scaling problems, this is the solution I will be using for the time being.
It has no side effects, scales very well due to its independence regarding other files and most importantly, once solution 3 becomes available to use, it will be very easy to simply replace the comments with a call to the import
keyword.
Conclusion
For the time being, solution 4 is the best practice. However, once browsers add support for solution 3, that should be used instead of solution 4.
Credits
Special thanks to Harutyun Amirjanyan for his tenacity, patience and guidance in helping me solve this issue.
这篇关于VarName未定义,请修复或添加/ *全局VarName * / Cloud9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!