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

前端 未结 11 675
无人共我
无人共我 2020-12-23 15:41

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

相关标签:
11条回答
  • 2020-12-23 16:08

    You can also use lodash assignIn which mutates the object. assignIn(obj, { someNewObj });

    https://lodash.com/docs/4.17.2#assignIn

    0 讨论(0)
  • 2020-12-23 16:10

    If you want to change any value inside an object array, you can use

    array.forEach(a => ({ ...a, expando: {} }))
    
    0 讨论(0)
  • 2020-12-23 16:11

    The no-param-reassign warning makes sense for common functions, but for a classic Array.forEach loop over an array which you intend to mutate it isn't to appropriate.

    However, to get around this, you can also use Array.map with a new object (if you are like me, dislike snoozing warnings with comments):

    someArray = someArray.map((_item) => {
        let item = Object.assign({}, _item); // decouple instance
        item.foo = "bar"; // assign a property
        return item; // replace original with new instance
    });
    
    0 讨论(0)
  • 2020-12-23 16:11
    function (el) {
      el.setAttribute('expando', {});
    }
    

    Everything else is just ugly hacks.

    0 讨论(0)
  • 2020-12-23 16:12

    You can override this rule inside your .eslintrc file and disable it for param properties like this

    {
        "rules": {
            "no-param-reassign": [2, { 
                "props": false
            }]
        },
        "extends": "eslint-config-airbnb"
    }
    

    This way rule is still active but it will not warn for properties. More info: http://eslint.org/docs/rules/no-param-reassign

    0 讨论(0)
  • 2020-12-23 16:23

    As this article explains, this rule is meant to avoid mutating the arguments object. If you assign to a parameter and then try and access some of the parameters via the arguments object, it can lead to unexpected results.

    You could keep the rule intact and maintain the AirBnB style by using another variable to get a reference to the DOM element and then modify that:

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

    In JS objects (including DOM nodes) are passed by reference, so here el and theElement are references to the same DOM node, but modifying theElement doesn't mutate the arguments object since arguments[0] remains just a reference to that DOM element.

    This approach is hinted at in the documentation for the rule:

    Examples of correct code for this rule:

    /*eslint no-param-reassign: "error"*/
    
    function foo(bar) {
        var baz = bar;
    }
    

    Personally, I would just use the "no-param-reassign": ["error", { "props": false }] approach a couple of other answers mentioned. Modifying a property of the parameter doesn't mutate what that parameter refers to and shouldn't run into the kinds of problems this rule is trying to avoid.

    0 讨论(0)
提交回复
热议问题