如何在DOM对象上设置属性时避免无参数重新分配

如何在DOM对象上设置属性时避免无参数重新分配

本文介绍了如何在DOM对象上设置属性时避免无参数重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,其主要目的是在DOM对象上设置一个属性

I have a method which's main purpose is to set a property on a DOM object

function (el) {
  el.expando = {};
}

我使用AirBnB的代码样式,使ESLint抛出一个 no-param-reassign 错误:

I use AirBnB's code style which makes ESLint throw a no-param-reassign error:

如何在符合AirBnB的代码样式的情况下操作作为参数传递的DOM对象?

How can I manipulate a DOM object passed as an argument while conforming AirBnB's code style?

有人建议使用 / * eslint react / prop-types:0 * / 引用,但如果我没有错误,这适用于反应,但不适用于本机DOM操作。

Somebody suggested to use /* eslint react/prop-types: 0 */ referring to another issue but if I am not mistaken this applies well for react, but not for native DOM manipulation.

此外,我不认为更改代码样式是一个答案。我相信使用标准风格的好处之一是在项目之间保持一致的代码,并改变规则会感觉像滥用AirBnB这样的主要代码样式。

Also I do not think changing the code style is an answer. I believe one of the benefits of using a standard style is having consistent code across projects and changing the rules at will feels like a misuse of a major code style like AirBnB's.

为了纪录,我问AirBnB在GitHub上,他们认为在这些情况下怎么样?。

For the record, I asked AirBnB on GitHub, what they think is the way to go in these cases in issue #766.

推荐答案

Mathletics建议,您可以,将其添加到您的 .eslintrc.json file:

As @Mathletics suggests, you can disable the rule entirely by adding this to your .eslintrc.json file:

"rules": {
  "no-param-reassign": 0
}

或者你可以禁用规则特别是对于参数属性

Or you could disable the rule specifically for param properties

"rules": {
  "no-param-reassign": [2, { "props": false }]
}

或者,您可以禁用该功能的规则

Alternatively, you could disable the rule for that function

/*eslint-disable no-param-reassign*/
function (el) {
  el.expando = {};
}
/*eslint-enable no-param-reassign*/

或者对于该行只有

function (el) {
  el.expando = {}; // eslint-disable-line no-param-reassign
}

您还可以检查关于禁用ESLint规则的此特别适用于AirBnB的风格指南。

You might also check out this blog post on disabling ESLint rules specifically to accommodate AirBnB's style guide.

这篇关于如何在DOM对象上设置属性时避免无参数重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:51