# Deep spread operator

**URL:** <https://es.discourse.group/t/deep-spread-operator/397>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [July 12, 2020, 2:25pm UTC](https://es.discourse.group/t/deep-spread-operator/397 "2020-07-12T14:25:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TatianaFomina](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@TatianaFomina](https://es.discourse.group/u/TatianaFomina)\
**Post date:** [July 12, 2020, 2:25pm UTC](https://es.discourse.group/t/deep-spread-operator/397/1 "2020-07-12T14:25:50Z")

</div>

There is no standardized way for creating deep copy of object. Nowadays developers have to use cloning functions from external libraries or implement them by themselves.

I propose to add **deep spread operator** to EcmaScript standart. This operator would extend features of regular spread operator with the difference that always will return recursive copy of its operand.

Proposed operator could have the following syntax:

```javascript
const deepCopy = {...#obj};

```

```javascript
const obj = {
  nestedArray: [1, 2, 3]
};

const shallowCopy = {...obj};
const deepCopy = {...#obj};

console.log(shallowCopy.nestedArray === obj.nestedArray); // true
console.log(deepCopy.nestedArray === obj.nestedArray); // false

```

---

<div class="post-metadata">

**Author:** ![kaizhu256](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kaizhu256/32/342_2.png) [@kaizhu256](https://es.discourse.group/u/kaizhu256)\
**Post date:** [July 16, 2020, 5:22pm UTC](https://es.discourse.group/t/deep-spread-operator/397/2 "2020-07-16T17:22:46Z")

</div>

you can do some of that already with JSON.parse(JSON.stringify(...))

```javascript
const obj = {
   nestedArray: [1, 2, 3]
};

const deepCopy = JSON.parse(JSON.stringify(obj));

```

drawback of current method is it won't properly clone datatypes undefined, bigint, symbol, (and exotic-objects set, map, weakmap, regexp, etc.).

how would your proposal handle deep-cloning pathological-cases llike map and set?

```javascript
let obj = new Map();
obj.set(new Set(), new Set());
obj.set(new Set(), new Set());
// obj = Map { Set {} => Set {}, Set {} => Set {} }

let deepCopy = {...#obj}
// deepCopy = ???

```

---

<div class="post-metadata">

**Author:** ![Jonas\_Wilms](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jonas_wilms/32/189_2.png) [@Jonas\_Wilms](https://es.discourse.group/u/Jonas_Wilms)\
**Post date:** [July 22, 2020, 5:50pm UTC](https://es.discourse.group/t/deep-spread-operator/397/3 "2020-07-22T17:50:16Z")

</div>

I like the idea, however what's the reason for the `...#` syntax? I think there are rather more intuitive ones, e.g. `...expression...`, and the `#` is already used for private fields so that causes confusion.

---

<div class="post-metadata">

**Author:** ![TatianaFomina](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@TatianaFomina](https://es.discourse.group/u/TatianaFomina)\
**Post date:** [July 23, 2020, 3:53pm UTC](https://es.discourse.group/t/deep-spread-operator/397/4 "2020-07-23T15:53:26Z")

</div>

This is good question. I think in Map case both keys and values should be copied by value. For abovementioned expample each _new Set()_ key would be replaced with its deep copy in resulting deepCopy object.

---

<div class="post-metadata">

**Author:** ![TatianaFomina](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@TatianaFomina](https://es.discourse.group/u/TatianaFomina)\
**Post date:** [July 23, 2020, 3:58pm UTC](https://es.discourse.group/t/deep-spread-operator/397/5 "2020-07-23T15:58:41Z")

</div>

Hi, selected syntax for new operator is not final and was only given as an example. So, I don't mind changing it.
