Closure Compiler Warning `dangerous use of the global this object`?

后端 未结 3 1423
北荒
北荒 2020-12-05 19:39

Dear folks, Closure Compiler gives this warnings in Advanced Mode, underlining {this.

JSC_USED_GLOBAL_THIS: dangerous use of the global this object at l

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 19:57

    "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])
    

提交回复
热议问题