In ESLint, the no-param-reassign rule as documented here, forbids you from assigning the value of a function param.
This is to avoid having a function's arguments
object be modified.
The correct way to code is to reassign the param to a local var
and return the var
. This is fine for some types, but it seems pointless for objects passed to a function.
For example, let's take this function;
function foo(param) {
var copy = param; // This makes the linter happy
copy.bar = 2;
console.log('arg 0: ', arguments[0], 'param:', param, 'copy:', copy);
return copy; // A pointless return, the original object has been modified.
}
let test = { bar: 1 };
foo(test);
console.log(test); // Has been modified
test = foo(test); // a pointless reassignment, foo has already changed test.
console.log(test); // Same effect as previous function call.
To be fair, ESLint does allow you to turn this feature off with /*eslint no-param-reassign: ["error", { "props": false }]*/
; but I have to wonder why?
The point of this rule is to get rid of mutability and keep the arguments
object pure, but a simple reassignment of an object will not do this.
The only way to truly do that would be to deep clone the param and assign that to a function scoped variable.
Am I missing something here?