EsLint-禁止“请勿使用'new'产生副作用” [英] EsLint - Suppress "Do not use 'new' for side effects"
问题描述
我看到了用jsLint抑制它的方法,尝试了一下,但是没有用。
我需要'new'关键字,或者我的脚本不起作用。 / p>
如何在.eslintrc中隐藏它?
非常感谢
更新:根据Jordan的要求。
[请注意,我的应用程序是用ReactJs编写的]
//第三方
const AnimateSlideShow = require('../ js-animation');
导出默认类Animate扩展React.Component {
.......
fetchJsAnimation(){
const animation = this.refs.Animation;
个新的AnimateSlideShow(animation);
}
......
}
错误:请勿将 new用于没有新出现的副作用
现在,如果我满足EsLint的要求,我的应用程序就会退出:
未捕获(承诺)TypeError:无法设置属性'_handleMouse'的undefined(…)
以下是ESLint规则的文档问题: http://eslint.org/docs/rules/no-new.html
禁止
new
产生副作用(没有新的
)
将
new
与构造函数一起使用的目标是通常创建一个特定类型的对象并将该对象存储在变量中,例如:var person = new Person();
使用
new
并不常见存储结果,例如:new Person();
在这种情况下,创建的对象将被丢弃,因为其引用未存储在任何地方。在很多情况下,这意味着构造函数应该替换为不需要使用new的函数。
我粘贴了以上是因为我认为了解规则的意图是很重要的,而不仅仅是了解如何使规则消失。
如果您找不到解决方法,摆脱 new
,可以使用 eslint-disable
指令抑制此错误:
fetchJsAnimation(){
/ * eslint-disable no new * /
const animation = this .refs.Animation;
个新的AnimateSlideShow(animation);
}
ESLint指令是块作用域的,因此仅在此函数中会被抑制。您还可以使用 eslint-disable-line
指令在一行上取消规则:
new AnimateSlideShow(animation); // eslint-disable-line no new
如果您确实需要整个禁用该规则项目,然后在您的 .eslintrc
的规则
部分中,将此规则的值设置为 0
:
{
//。 ..
规则:{
no-new:0,
// ...
}
}
您也可以通过将其设置为 1
来警告而不是错误( 2
是错误的。)
I saw the way to suppress this with jsLint, tried it, it did not work.
I need the 'new' keyword or my script does notwork.
How can I suppress it in .eslintrc?
Many Thanks
Update: Per Jordan's request. [Please note my app is written in ReactJs]
// 3rd party
const AnimateSlideShow = require('../js-animation');
export default class Animate extends React.Component {
.......
fetchJsAnimation() {
const animation = this.refs.Animation;
new AnimateSlideShow(animation);
}
......
}
Error: Do not use 'new' for side effects no-new
Now, if I satisfy EsLint, my app craps out:
Uncaught (in promise) TypeError: Cannot set property '_handleMouse' of undefined(…)
Here's the documentation for the ESLint rule in question: http://eslint.org/docs/rules/no-new.html
Disallow
new
For Side Effects (no-new
)The goal of using
new
with a constructor is typically to create an object of a particular type and store that object in a variable, such as:var person = new Person();
It's less common to use
new
and not store the result, such as:new Person();
In this case, the created object is thrown away because its reference isn't stored anywhere, and in many cases, this means that the constructor should be replaced with a function that doesn't require new to be used.
I pasted that above because I think it's important to understand what the intent of the rule is, and not just how to make it go away.
If you can't find a way to get rid of new
, you can suppress this error with the eslint-disable
directive:
fetchJsAnimation() {
/* eslint-disable no-new */
const animation = this.refs.Animation;
new AnimateSlideShow(animation);
}
ESLint directives are block-scoped, so it will be suppressed inside this function only. You can also suppress rules on a single line with the eslint-disable-line
directive:
new AnimateSlideShow(animation); // eslint-disable-line no-new
If you really need to disable this rule for your entire project, then in your .eslintrc
's "rules"
section set the value for this rule to 0
:
{
// ...
"rules": {
"no-new": 0,
// ...
}
}
You can also make it a warning instead of an error by setting it to 1
(2
is error).
这篇关于EsLint-禁止“请勿使用'new'产生副作用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!