Skip to content

Commit 0f3dea4

Browse files
committed
Object.assign
1 parent 14de7dc commit 0f3dea4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,52 @@ function writeForumComment(subject='No subject', body='No text') {
205205
```
206206
**[⬆ back to top](#table-of-contents)**
207207

208+
### Set default objects with Object.assign
209+
210+
**Bad:**
211+
```javascript
212+
var menuConfig = {
213+
title: null,
214+
body: 'Bar',
215+
buttonText: null
216+
cancellable: true
217+
}
218+
219+
function createMenu(config) {
220+
config.title = config.title || 'Foo'
221+
config.body = config.body || 'Bar'
222+
config.buttonText = config.title || 'Baz'
223+
config.cancellable = config.cancellable === undefined ? config.cancellable : true;
224+
225+
}
226+
227+
createMenu(menuConfig);
228+
```
229+
230+
**Good**:
231+
```javascript
232+
var menuConfig = {
233+
title: null,
234+
body: 'Bar',
235+
buttonText: null
236+
cancellable: true
237+
}
238+
239+
function createMenu(config) {
240+
Object.assign(config, {
241+
title: 'Foo',
242+
body: 'Bar',
243+
buttonText: 'Baz',
244+
cancellable: true
245+
});
246+
}
247+
248+
createMenu(menuConfig);
249+
```
250+
**[⬆ back to top](#table-of-contents)**
251+
252+
253+
208254
### Don't use flags as function parameters
209255
Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean.
210256

0 commit comments

Comments
 (0)