如何处理JSLint警告“不要对副作用使用‘new’?”

时间:2021-05-04 19:42:28

Why do I get these errors?

为什么会出现这些错误?

Problem at line 329 character 60: Do not use 'new' for side effects.

问题在第329行字符60:不要使用“new”作为副作用。

new widget.StyledDropdown(dojo.byId("sTitle"));

新widget.StyledDropdown(dojo.byId(“sTitle”);

Problem at line 330 character 61: Do not use 'new' for side effects.

问题在第330行第61个字符:不要对副作用使用“new”。

new widget.StyledDropdown(dojo.byId("sSuffix"));

新widget.StyledDropdown(dojo.byId(“sSuffix”);

Problem at line 336 character 57: Do not use 'new' for side effects.

问题在第336行第57个字符:不要使用“new”作为副作用。

true,{shortenName : true,maxChars : 20});

true,{shortenName: true,maxChars: 20});

Problem at line 338 character 129: Do not use 'new' for side effects.

第338行问题129:不要使用“new”作为副作用。

new widget.StyledDropdown(dojo.byId("sCountry"),USPS.Address.countrySw...

新widget.StyledDropdown(dojo.byId(“sCountry”),USPS.Address.countrySw……

2 个解决方案

#1


21  

You're not storing a reference to the newly-created objects, which is a code smell.

您不会存储对新创建对象的引用,这是一种代码味道。

JSLint is saying "You're creating some objects but immediately discarding them; the only possible reason you can be doing that is that the act of creating the objects has side-effects, which is weird."

JSLint说的是“您正在创建一些对象,但是立即丢弃它们;你这么做的唯一可能的原因是创建对象的行为有副作用,这很奇怪。

You can lose the warning either by preventing your constructors having side effects (which will mean finding some other way of doing whatever it is they're doing, eg. by moving that code into a normal function) or by storing references to the newly-created objects (even in a temporary local variable that you discard).

你可能会因为阻止你的构造函数产生副作用而失去警告(这意味着你会找到其他方法来做他们正在做的事情,例如。通过将代码移动到一个普通的函数中)或存储对新创建对象的引用(甚至在您丢弃的临时局部变量中)。

#2


1  

Rethinking the strategy is best, but more often, handling tech debt during a development cycle isn't an option.

重新思考战略是最好的,但更常见的是,在开发周期中处理技术债务不是一种选择。

If you are using JSHint you can override this option on a case by case basis. Add this jshint comment in the scope of the offending code.

如果您使用的是JSHint,您可以在一个案例的基础上覆盖这个选项。将这个jshint注释添加到违规代码的范围中。

/* jshint -W031 */
new widget.StyledDropdown(dojo.byId("sTitle"));
new widget.StyledDropdown(dojo.byId("sSuffix"));
...

Inline configurations are function scoped. So anything outside the scope of the comment will still be checked.

内联配置具有函数作用域。因此,任何超出注释范围的内容仍将被检查。

#1


21  

You're not storing a reference to the newly-created objects, which is a code smell.

您不会存储对新创建对象的引用,这是一种代码味道。

JSLint is saying "You're creating some objects but immediately discarding them; the only possible reason you can be doing that is that the act of creating the objects has side-effects, which is weird."

JSLint说的是“您正在创建一些对象,但是立即丢弃它们;你这么做的唯一可能的原因是创建对象的行为有副作用,这很奇怪。

You can lose the warning either by preventing your constructors having side effects (which will mean finding some other way of doing whatever it is they're doing, eg. by moving that code into a normal function) or by storing references to the newly-created objects (even in a temporary local variable that you discard).

你可能会因为阻止你的构造函数产生副作用而失去警告(这意味着你会找到其他方法来做他们正在做的事情,例如。通过将代码移动到一个普通的函数中)或存储对新创建对象的引用(甚至在您丢弃的临时局部变量中)。

#2


1  

Rethinking the strategy is best, but more often, handling tech debt during a development cycle isn't an option.

重新思考战略是最好的,但更常见的是,在开发周期中处理技术债务不是一种选择。

If you are using JSHint you can override this option on a case by case basis. Add this jshint comment in the scope of the offending code.

如果您使用的是JSHint,您可以在一个案例的基础上覆盖这个选项。将这个jshint注释添加到违规代码的范围中。

/* jshint -W031 */
new widget.StyledDropdown(dojo.byId("sTitle"));
new widget.StyledDropdown(dojo.byId("sSuffix"));
...

Inline configurations are function scoped. So anything outside the scope of the comment will still be checked.

内联配置具有函数作用域。因此,任何超出注释范围的内容仍将被检查。