关闭编译器警告`危险使用全局这个对象? [英] Closure Compiler Warning `dangerous use of the global this object`?

查看:112
本文介绍了关闭编译器警告`危险使用全局这个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人,Closure Compiler在高级模式下给出了这个警告,强调了 {this。



JSC_USED_GLOBAL_THIS:dangerous使用全局此对象在行200字符33
hovers [i4] .onfocus = function(){this.className + =Hovered;} / p>

JSC_USED_GLOBAL_THIS:危险使用全局此对象在行201字符32
hovers [i4] .onblur = function(){this .className = this.className.replace(/ Hove ...



JSC_USED_GLOBAL_THIS:危险使用全局此对象在行201字符49
hovers [i4] .onblur = function(){this.className = this.className.replace(/ Hove ...



JSC_USED_GLOBAL_THIS:危险使用全局此对象在行218字符38
按钮[i5] .onmouseover = function(){this.className + =Hovered ;}



Q1。对此有什么危险?

Q2。我应该改变吗? / strong>

Q3。如何改善/解决这个代码?



merci!

解决方案

这个可能在不同的上下文中有不同的含义,所以它告诉你。
您可以使用闭包:



而不是

  hovers [i4] .onfocus = function(){this.className + =Hovered;} 

有:

  hovers [i4] .onfocus = function(self)
{
return function ){self.className + =Hovered;}
}(hovers [i4])


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 33 hovers[i4].onfocus = function() {this.className += "Hovered";}

JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 201 character 32 hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...

JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 201 character 49 hovers[i4].onblur = function() {this.className = this.className.replace(/Hove...

JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 218 character 38 buttons[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!

解决方案

"this" might have different meaning in different context, so it tells you exactly that. You can use closures instead:

Instead of

hovers[i4].onfocus = function() {this.className += "Hovered";}

have:

hovers[i4].onfocus = function(self) 
{
    return function() {self.className += "Hovered";}
}(hovers[i4])

这篇关于关闭编译器警告`危险使用全局这个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆