File tree
Expand file treeCollapse file tree1 file changed
+26
-0
lines changed Expand file treeCollapse file tree1 file changed
+26
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -550,6 +550,32 @@ console.log(newName); // ['Ryan', 'McDermott'];
|
550 | 550 | ```
|
551 | 551 | **[⬆ back to top](#table-of-contents)**
|
552 | 552 |
|
| 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 | + |
553 | 579 | ### Don't write to global functions
|
554 | 580 | Polluting globals is a bad practice in JavaScript because you could clash with another
|
555 | 581 | library and the user of your API would be none-the-wiser until they get an
|
|
You can’t perform that action at this time.
0 commit comments