我看到了用jsLint抑制它的方法,尝试了一下,但是没有用。
我需要'new'关键字,否则我的脚本不起作用。
如何在.eslintrc中禁止显示?
非常感谢
更新:根据Jordan的要求。
[请注意我的应用程序是用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
现在,如果我满足EsLint的要求,我的应用程序就会退出:
Uncaught (in promise) TypeError: Cannot set property '_handleMouse' of undefined(…)
最佳答案
以下是有关ESLint规则的文档: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.
我将其粘贴在上面,是因为我认为了解规则的意图很重要,而不仅仅是如何使它消失。
如果找不到摆脱
new
的方法,则可以使用eslint-disable
指令抑制此错误:fetchJsAnimation() {
/* eslint-disable no-new */
const animation = this.refs.Animation;
new AnimateSlideShow(animation);
}
ESLint指令是块作用域的,因此仅在此函数内将被禁止。您还可以使用
eslint-disable-line
指令在一行上取消显示规则:new AnimateSlideShow(animation); // eslint-disable-line no-new
如果确实需要为整个项目禁用此规则,则在
.eslintrc
的"rules"
部分中,将此规则的值设置为0
:{
// ...
"rules": {
"no-new": 0,
// ...
}
}
您也可以通过将其设置为
1
(2
是错误的)来警告而不是出错。
关于eslint - EsLint-抑制 “Do not use ' new'的副作用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33287045/