Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

javascript - How to avoid no-param-reassign when setting a property on a DOM object

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

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

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

error Assignment to function parameter 'el' no-param-reassign

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

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.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 */

Or for that line only

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...