Create shallow-clone-object.md

We need a deep clone too but that's a different animal.

`JSON.parse(JSON.stringify(obj))` is not very good because it strips functions, etc.
This commit is contained in:
atomiks
2017-12-15 08:17:21 +11:00
committed by GitHub
parent 0bbe323f70
commit 28bc2bef7c

View File

@ -0,0 +1,12 @@
### Shallow clone object
Use the object spread operator to spread the properties of the target object into the clone.
```js
const shallowClone = obj => ({ ...obj });
/*
const a = { x: true, y: 1 };
const b = shallowClone(a);
a === b -> false
*/
```