eslint - EsLint-抑制 “Do not use ' new'的副作用”

标签 eslint

我看到了用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/

相关文章:

vue.js - Eslint Vue 3 解析错误 : '>' expected.

javascript - 在 vuejs 中使用全局函数。 Lint (?)

visual-studio-code - 如何让 VSCode 使用全局 eslint 而不是本地 eslint

javascript - 使用 webpack 别名显示的 eslint 错误

javascript - 如何使用 ESLint 通过特定文件夹的绝对路径限制模块的导入

reactjs - 通过 ESLint 版本在 vscode 上启动 React 应用程序失败

javascript - 自定义 eslint 规则抛出意外保留字

javascript - 箭头函数解析错误: Unexpected token >

javascript - 是否有针对 IE8/IE9 的 ESLint 预制配置文件?

javascript - 为什么它是以下代码中的竞争条件?