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

发布于 2024-12-07 13:48:35 字数 421 浏览 0 评论 0原文

我正在使用 Resharper 6 和 ASP.NET Web 方法,并且在我的 Javascript 文件中出现令人恼火的警告:

"Use of implicitly declared global variable 'X'"

原因是 Web 方法在 Javascript 中创建为:

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

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

我的问题是:如何消除这种情况下的错误,而不是消除合理错误的情况?

谢谢!

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

"Use of implicitly declared global variable 'X'"

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

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.

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

Thanks!

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

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

发布评论

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

评论(4

淡看悲欢离合 2024-12-14 13:48:35

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

multipleContainer

正如您从屏幕截图中看到的,ReSharper (6.0.2202.688) 对 jQuery 很满意, ContainerAContainerB 即使它们没有在当前文件中的任何位置定义。第 1 行中的注释仅适用于 JSLint(无错误)。

此技术假设所有其他 JavaScript 文件都遵循 JavaScript 最佳实践,即通过定义包含所有导出(public)符号(即 jQuery 是 jQuery 库及其插件的唯一全局对象,ContainerA 是 LibraryA 的唯一全局对象,ContainerB 是 LibraryB 的唯一全局对象,等等) 。

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

窗口作为容器

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

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:

multiple containers

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

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. jQuery is the only global object for jQuery library and its plugins, ContainerA is the only global object for LibraryA, ContainerB is the only global object for LibraryB, etc).

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:

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.X into your code. Instead, your code is instantiating classes from the aspNet container (that at the moment happens to be a synonym for window, but that might change in the future). Also, having aspNet.X in 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.

冰之心 2024-12-14 13:48:35

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

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

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.

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.

玩套路吗 2024-12-14 13:48:35

将 Jasmine 移动到外部 Bower 包并从 VS 项目中排除 Jasmine 的代码后,遇到了完全相同的问题。 Resharper 立即开始抱怨使用隐式声明的全局变量“describe”等。

我通过向项目添加另一个名为 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 () { };

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

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.

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

// 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.

鹊巢 2024-12-14 13:48:35

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

// ReSharper disable once UseOfImplicitGlobalInFunctionScope

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

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