Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e881277

Browse files
authored
Add Side Effects pt.2
1 parent cd50889 commit e881277

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,32 @@ console.log(newName); // ['Ryan', 'McDermott'];
550550
```
551551
**[⬆ back to top](#table-of-contents)**
552552

553+
### Avoid Side Effects pt.2
554+
Side effects could also occur from inside a function. In Javascript, function arguments
555+
are always passed by value except when they(functions) are passed reference values such as
556+
objects and arrays. In that case, we should be carefull not to change any of these
557+
argument's properties. A possible solution would be to always clone the variable,
558+
edit it and return the clone.
559+
560+
**Bad:**
561+
```javascript
562+
function userReducer(state, action) {
563+
state.userProfile = action.payload;
564+
return state;
565+
}
566+
```
567+
568+
**Good:**
569+
```javascript
570+
function userReducer(state, action) {
571+
var s = Object.assign({}, state);
572+
s.userProfile = action.payload;
573+
return s;
574+
}
575+
```
576+
577+
**[⬆ back to top](#table-of-contents)**
578+
553579
### Don't write to global functions
554580
Polluting globals is a bad practice in JavaScript because you could clash with another
555581
library and the user of your API would be none-the-wiser until they get an

0 commit comments

Comments
 (0)