闭包编译器警告“全局 this 对象的危险使用”?
亲爱的朋友们,闭包编译器在高级模式下给出了此警告,并在 {this.
JSC_USED_GLOBAL_THIS:第 200 行第 33 行字符处危险使用全局 this 对象 hovers[i4].onfocus = function() {this.className += "Hovered";}
JSC_USED_GLOBAL_THIS:在第 201 行第 32 行字符处危险使用全局 this 对象 hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...
JSC_USED_GLOBAL_THIS: 在第 201 行第 49 行危险使用全局 this 对象 hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...
JSC_USED_GLOBAL_THIS: 在第 218 行第 38 行危险使用全局 this 对象 buttons[i5].onmouseover = function() {this.className += "Hovered";}
Q1.这有什么危险?
第二季度。我应该改变这个吗?
第三季度。我该如何改进/解决这个代码?
谢谢!
Dear folks, Closure Compiler gives this warnings in Advanced Mode, underlining {this.
JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 200 character 33hovers[i4].onfocus = function() {this.className += "Hovered";}
JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 201 character 32hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...
JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 201 character 49hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...
JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 218 character 38buttons[i5].onmouseover = function() {this.className += "Hovered";}
Q1. Whats so dangerous about this?
Q2. Should I change this?
Q3. How do I improve/solve this code?
merci!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道“this”变量的类型,则可以使用 JsDoc 声明它以阻止编译器抱怨:
警告:但是,假设您确实知道“this”的类型“ 多变的。这可能并不像看起来那么容易。例如:
您应该知道
doSomething
中的“this”指的是foo
。但是,如果您使用闭包编译器的高级模式,编译器可能会“扁平化”foo
命名空间,并且最终会得到:with
foo. doSomething
被“扁平化”为单个全局变量a
。在这种情况下,“this”变量显然指向全局对象!你的代码将会被破坏!因此,闭包编译器非常坚决地警告您不要在可以扁平化的函数中使用“this”。不过,您可以在构造函数和原型函数中使用“this”,而不会出现此警告。
要解决此问题,最好通过使用命名空间本身来避免使用“this”:
If you know the type of the "this" variable, you can declare it with a JsDoc to stop the compiler from complaining:
Caveat: this, however, assumes you know for sure the type of the "this" variable. This may not be as easy as it seems. For example:
You would have known that "this" in
doSomething
refers tofoo
. However, if you use the Advanced Mode of the Closure Compiler, the compiler may "flatten" thefoo
namespace and you'll end up with:with
foo.doSomething
being "flattened" to a single global variablea
. In this case, the "this" variable obviously points to the global object instead! Your code will break!Therefore, the Closure Compiler is quite adamant in warning you not to use "this" in functions that can be flattened. You may use "this" in constructors and prototype functions without this warning though.
To resolve this, it is better to avoid using "this" by using the namespace itself:
“this”在不同的上下文中可能有不同的含义,所以它准确地告诉你这一点。
您可以使用闭包代替:
而不是
有:
"this" might have different meaning in different context, so it tells you exactly that.
You can use closures instead:
Instead of
have:
只是添加 @marcinkuzminski 在 @stephen Chung 答案中添加评论的示例
来源:https://developers .google.com/closure/compiler/docs/js-for-compiler
Just to add example of what @marcinkuzminski added comment to @stephen Chung answer
Source : https://developers.google.com/closure/compiler/docs/js-for-compiler