Resharper,Javascript:“使用隐式声明的全局变量‘X’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7604419/
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-08-24 02:52:37  来源:igfitidea点击:

Resharper, Javascript: "Use of implicitly declared global variable 'X'"

javascriptresharper

提问by rythos42

I'm using Resharper 6 and ASP.NET Web Methods and have an irritating warning in my Javascript files:

我正在使用 Resharper 6 和 ASP.NET Web Methods,并且在我的 Javascript 文件中有一个令人讨厌的警告:

"Use of implicitly declared global variable 'X'"

The reason is that the web method is created in Javascript as:

原因是 web 方法是在 Javascript 中创建的:

new X.example().webMethod(arg1, arg2, successCallback, failureCallback);

And X...is implicitly defined. I'm wondering if there is a solution to explicitly define this? It's defined in some auto-generated JS file, created by the ASP.NET web method framework stuff.

而 X... 是隐式定义的。我想知道是否有解决方案来明确定义这一点?它在一些自动生成的 JS 文件中定义,由 ASP.NET Web 方法框架的东西创建。

My question is: how do I get rid of the error for this situation, without getting rid of it for legitimately wrong situations?

我的问题是:我如何摆脱这种情况的错误,而不是在合法错误的情况下摆脱它?

Thanks!

谢谢!

采纳答案by Milan Gardian

When using symbols (functions, constants, global variables) defined in other JavaScript files, I pass them to the current file's "scoping function" (top-level, usually anonymous, function that prevents global namespace pollution) as parameters:

当使用其他 JavaScript 文件中定义的符号(函数、常量、全局变量)时,我将它们作为参数传递给当前文件的“作用域函数”(顶级的,通常是匿名的,防止全局命名空间污染的函数):

multiple containers

多个容器

As you can see from the screenshot, ReSharper (6.0.2202.688) is happy with jQuery, ContainerAand ContainerBeven though they are not defined anywhere in the current file. The comment in line 1 is only there for JSLint (no errors).

正如你从截图中可以看到,ReSharper的(6.0.2202.688)很满意jQueryContainerAContainerB即使他们没有在当前文件的任何地方定义。第 1 行中的注释仅适用于 JSLint(没有错误)。

This technique assumes that all the other JavaScript files follow the JavaScript best practice of minimally polluting the global namespace by defining a single top-level object that contains all the exported (public) symbols (i.e. jQueryis the only global object for jQuery library and its plugins, ContainerAis the only global object for LibraryA, ContainerBis the only global object for LibraryB, etc).

这种技术假设所有其他 JavaScript 文件都遵循 JavaScript 最佳实践,即通过定义一个包含所有导出(公共)符号的顶级对象(即jQueryjQuery 库及其插件的唯一全局对象)来最小化全局命名空间的污染,ContainerA是 LibraryAContainerB的唯一全局对象,是 LibraryB 的唯一全局对象,等等)。

Because you clearly don't have control over ASP.NET Web Methods generating constructor functions into global namespace, in your case you have to resort to the ultimate container, window:

因为您显然无法控制将构造函数生成到全局命名空间的 ASP.NET Web 方法,所以在您的情况下,您必须求助于最终容器window

window as a container

窗口作为容器

This is a slight variation on the technique suggested by @sethobrien in his comment. An important (IMHO) advantage is that you're not hard-coding window.Xinto your code. Instead, your code is instantiating classes from the aspNetcontainer (that at the moment happens to be a synonym for window, but that might change in the future). Also, having aspNet.Xin the code declares your intention more clearly for people who will read your code in the future. Finally, local variables can be shortened by JavaScript minimizers yielding slightly smaller file transmitted to client browsers.

这是@sethobien 在他的评论中建议的技术的轻微变化。一个重要的(恕我直言)优势是您不会硬编码window.X到您的代码中。相反,您的代码正在实例化aspNet容器中的类(目前恰好是 的同义词window,但将来可能会发生变化)。此外,aspNet.X在代码中为将来会阅读您代码的人更清楚地声明您的意图。最后,局部变量可以通过 JavaScript 最小化器来缩短,从而产生稍小的文件传输到客户端浏览器。

回答by Ilia Barahovski

Got exactly the same problem after moving Jasmine to an external Bower package and excluding Jasmine's code from VS project. Resharper immediately started complaining on ?Use of an implicitly declared global variable 'describe'and so on.

将 Jasmine 移动到外部 Bower 包并从 VS 项目中排除 Jasmine 的代码后,遇到了完全相同的问题。Resharper 立即开始抱怨?Use of an implicitly declared global variable 'describe'等等。

I solved this by adding to the project another file named workaround.jsdummy definitions for the variables. In your case it would be:

我通过向项目添加另一个名为workaround.js变量的虚拟定义的文件来解决这个问题。在您的情况下,它将是:

// This is a workaround for R# complaining on undefined global variables.
// In practice they come from and are defined by external frameworks, so 
// this is not a real issue.

var X = function () { };

And this is a file in my project - https://gist.github.com/barahilia/62871d9219cee825d82e.

这是我项目中的一个文件 - https://gist.github.com/barahilia/62871d9219cee825d82e

回答by Alexei Levenkov

Adding following to the top of your script file ///<reference path="my.js" />(my.js is the file where X is defined) will likely fix this warning since ReSharper start seeing this global variable.

将以下内容添加到脚本文件的顶部///<reference path="my.js" />(my.js 是定义 X 的文件)可能会修复此警告,因为 ReSharper 开始看到此全局变量。

Otherwise to minimize changes you can add var X = window.X;near the top of the file. Try to make it not to polute global namespace and make sure it will not confuse code that actually instantiates X on the window.

否则,为了尽量减少更改,您可以var X = window.X;在文件顶部附近添加。尽量让它不污染全局命名空间,并确保它不会混淆在窗口上实际实例化 X 的代码。

回答by floralGhost

If it is an error that can be ignored you can use

如果这是一个可以忽略的错误,你可以使用

// ReSharper disable once UseOfImplicitGlobalInFunctionScope