在 DOM 对象上设置属性时如何避免无参数重新分配 [英] How to avoid no-param-reassign when setting a property on a DOM object
问题描述
我有一个主要目的是在 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:
错误分配给函数参数'el' no-param-reassign
error Assignment to function parameter 'el' no-param-reassign
如何在符合 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.
作为记录,我在 GitHub 上询问了 AirBnB,他们认为在这些情况下的方法是什么 在问题 #766 中.
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
文件中:
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
}
这篇关于在 DOM 对象上设置属性时如何避免无参数重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!