关闭编译器警告`危险使用全局这个对象? [英] Closure Compiler Warning `dangerous use of the global this object`?
问题描述
{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屋!