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

前端 未结 11 676
无人共我
无人共我 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:24

    You can use methods for updating data. Eg. "res.status(404)" instead of "res.statusCode = 404" I found the solution. https://github.com/eslint/eslint/issues/6505#issuecomment-282325903

    /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["$scope"] }]*/
    
    app.controller('MyCtrl', function($scope) {
      $scope.something = true;
    });
    
    0 讨论(0)
  • 2020-12-23 16:26

    Those wishing to selectively deactivate this rule might be interested in a proposed new option for the no-param-reassign rule that would allow a "white list" of object names with respect to which parameter reassignment should be ignored.

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

    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
    }
    

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

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

    Following the documentation:

    function (el) {
      const element = el
      element.expando = {}
    }
    
    0 讨论(0)
  • 2020-12-23 16:32

    You can use:

    (param) => {
      const data = Object.assign({}, param);
      data.element = 'some value';
    }
    
    0 讨论(0)
提交回复
热议问题