由于未定义的变量,JSLint 文件失败

发布于 2024-11-06 02:54:00 字数 773 浏览 0 评论 0原文

我刚刚开始将 JSLint 放入我的构建管道中,效果非常好。尽管它在我的大多数文件中指出了一些并不是错误的内容,但它会将其视为错误。我已经更改了我的构造函数,现在采用该对象的实例,以便测试通过,但是我不确定我是否真的应该这样做,因为在所有其他主要语言中我不需要这样做。

我将不得不为此添加更多背景信息,以便它有意义,所以这里是。

我有我能得到的最接近 JavaScript 中的 Enum 的东西,它基本上是一个全局范围的 JSON 样式变量,带有大量常量,它用于描述事件类型,而不是每个想要引发/监听事件的类都必须输入硬编码字符串,它只能使用此 enum 变量中的常量。正如我刚才提到的,我有这些类使用这个静态枚举,但它们只是使用这个变量的全局版本,而不是通过构造函数传递的本地实例,这就是我的位置问题开始了,就像在实际的应用程序中一样,我知道枚举文件将包含在内,这将使其可以全局访问。然而 JSLint 没有这方面的上下文,所以它只看到一个单独的文件,而不用担心外部依赖,因为它认为这些是不好的,这在任何其他语言中都是如此,但在 JS 中,如果没有全局依赖,你就无法实现同样的事情变量(据我所知)。

正如我最初所说,我现在已将此枚举添加到构造函数中,以让 JSLint 传递文件,但是传递它感觉有点错误,但也许这是因为我将其视为常规开发人员而不是 javascript开发人员...

现在我应该坚持这一点,并将其传递给构造函数,然后在我的测试中模拟它,还是应该采取它应该始终存在的方法?

我确信这取决于人们的个人意见,但很高兴知道我是否是个白痴,应该将每个文件保留为自己的孤岛,或者是否有办法让我鱼与熊掌兼得它。

I have just started putting JSLint into my build pipeline and it has been great. Although it has pointed out something in most of my files that is not an error as such, although it will see it as one. I have changed my constructor to now take an instance of this object so the tests pass, however I am not sure if I really should, as in all other major languages I would not need to do this.

I will have to add some more context to this, for it to make any sense so here goes.

I have the closest thing I can get to an Enum in javascript which is basically a globally scoped JSON style variable with a load of constants, it is used to describe event types so rather than every class that wants to raise/listen to events having to put hard coded strings, it can just use a constant from this enum variable. As I just mentioned I have these classes that make use of this static enum, but they just make use of the global version of this variable, not a local instance passed through the constructor, and this is where my problems begin, as in the actual app I know for a fact that the enum file will included which will make it globally accessible. However JSLint has no context of this, so it only sees an individual file without worrying about external dependencies, as it deems these to be bad, which in any other language would be true, but in JS you cannot achieve the same thing without having global variables (to my knowledge).

As I said originally, I have now added this enum to the constructor to let JSLint pass the files, however it just feels a bit wrong passing it in, but maybe this is because I am thinking about it as a regular developer and not a javascript developer...

Now should I stick to this, and pass it through the constructor, and just mock it in my tests, or should I take the approach that it should always be there?

I am sure this will be down to peoples personal opinions, but it would be nice to know if I am being an idiot and should just keep each file as its own silo, or if there is a way for me to have my cake and eat it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-11-13 02:54:00

我发现 JSLint 支持注释来告诉它全局变量:

/*globals myGlobal*/

我决定只使用我的枚举作为全局变量并继续处理更重要的事情。

I found that JSLint supports a comment to tell it of global variables:

/*globals myGlobal*/

I have decided to just use my enum as a global and get on with the more important things.

妞丶爷亲个 2024-11-13 02:54:00

JavascriptLint 在这方面极大地改进了 JSLint,因为它允许您定义文件间依赖关系:

如果脚本引用另一个脚本中的变量、函数或对象,您需要在脚本中添加 /*jsl:import PathToOtherScript*/ 注释。这告诉 JavaScript Lint 检查其他脚本中声明的项目。根据当前脚本的路径解析相对路径。

请参阅:http://javascriptlint.com/docs/index.htm

JavascriptLint greatly improves on JSLint in this respect, as it allows you to define inter-file dependencies:

If a script references a variable, function, or object from another script, you will need to add a /*jsl:import PathToOtherScript*/ comment in your script. This tells JavaScript Lint to check for items declared in the other script. Relative paths are resolved based on the path of the current script.

See: http://javascriptlint.com/docs/index.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文