update best-practices config to prevent parameter object manipulation

added good/bad examples of parameter mutation to the readme
This commit is contained in:
Thomas Shafer
2015-12-16 15:40:26 -08:00
parent fcc41eecd7
commit 2589c67b0c
2 changed files with 31 additions and 1 deletions

View File

@@ -629,6 +629,35 @@ Other Style Guides
const y = function a() {};
```
- [7.12](#7.12) <a name="7.12"></a> Never mutate parameters.
> Why? Overwriting parameters can lead to unexpected behavior, especially when accessing the `arguments` object. Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller.
eslint rules: [`no-param-reassign`](http://eslint.org/docs/rules/no-param-reassign.html).
```javascript
// bad
function f(a){
a = 1;
}
function f(a){
if (!a) { a = 1; }
}
function f(obj){
obj.key = 1;
};
// good
function f(a){
const b = (a || 1);
}
function f(a = 1){
}
function f(obj){
const key = obj.hasOwnProperty('key') ? obj.key ? 1;
};
```
**[⬆ back to top](#table-of-contents)**
## Arrow Functions

View File

@@ -74,7 +74,8 @@ module.exports = {
// var foo = 'Copyright \251';
'no-octal-escape': 2,
// disallow reassignment of function parameters
'no-param-reassign': 2,
// disallow parameter object manipulation
'no-param-reassign': [2, { 'props': true }],
// disallow use of process.env
'no-process-env': 0,
// disallow usage of __proto__ property