mirror of
https://github.com/labs42io/clean-code-typescript.git
synced 2024-11-23 05:34:04 +00:00
add example with spread operator
This commit is contained in:
parent
d2ac9bf873
commit
7aecafe532
18
README.md
18
README.md
|
@ -622,6 +622,24 @@ function createMenu(config: MenuConfig) {
|
|||
createMenu({ body: 'Bar' });
|
||||
```
|
||||
|
||||
Or, you could use the spread operator:
|
||||
|
||||
```ts
|
||||
function createMenu(config: MenuConfig) {
|
||||
const menuConfig = {
|
||||
title: 'Foo',
|
||||
body: 'Bar',
|
||||
buttonText: 'Baz',
|
||||
cancellable: true,
|
||||
...config,
|
||||
};
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
The spread operator and `Object.assign()` are very similar.
|
||||
The main difference is that spreading defines new properties, while `Object.assign()` sets them. More detailed, the difference is explained in [this](https://stackoverflow.com/questions/32925460/object-spread-vs-object-assign) thread.
|
||||
|
||||
Alternatively, you can use destructuring with default values:
|
||||
|
||||
```ts
|
||||
|
|
Loading…
Reference in a new issue