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

前端 未结 11 731
无人共我
无人共我 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: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
    });
    

提交回复
热议问题