mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-14 10:38:03 -05:00
update best-practices config to prevent parameter object manipulation
added good/bad examples of parameter mutation to the readme
This commit is contained in:
29
README.md
29
README.md
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user