# Deep copy object in Javascript?

**URL:** <https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890>\
**Category:** Support\
**Created:** [July 29, 2016, 5:08am UTC](https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890 "2016-07-29T05:08:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [July 29, 2016, 5:08am UTC](https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890/1 "2016-07-29T05:08:01Z")

</div>

With plain JS objects, you can create a simple deep copy like this:

```
var copy = JSON.parse(JSON.stringify(obj));

```

If I do this with an instance of a class that’s written in Kotlin, is it going to cause problems? Maybe I need to write custom `copy` methods for my classes.

thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![molchanoviv](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/molchanoviv/32/1846_2.png) [@molchanoviv](https://discuss.kotlinlang.org/u/molchanoviv)\
**Post date:** [July 29, 2016, 11:04am UTC](https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890/2 "2016-07-29T11:04:06Z")

</div>

Serialization and deserialization of object only in order to copy new instance is a very ugly hack. Never use it even in js. If you want to deep copy your objects you need to implement Cloneable interface

---

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [July 29, 2016, 12:42pm UTC](https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890/3 "2016-07-29T12:42:08Z")

</div>

Cloneable is not available in the JS backend (see [KT-6639](https://youtrack.jetbrains.com/issue/KT-6639)). I’ll probably just implement a custom `copy` method.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [August 4, 2016, 4:04pm UTC](https://discuss.kotlinlang.org/t/deep-copy-object-in-javascript/1890/4 "2016-08-04T16:04:45Z")

</div>

It would be really nice if the data class generated copy() method was deep. I understand that Any doesn’t have ‘copy’, but the compiler could check for a copy() method on the data class’s members when auto-generating the copy() method.

(And yes, I know that you can always write your own copy() method, but that’s tedious and why we’re lazy and use serialization to clone!)

data class Foo(val a: Int, val b: Bar) {

// It’d be awesome if the compiler could generate the copy method like this:  
fun copy(): Foo {  
return Foo(a, b.copy())  
}

// Instead of how it is now:  
fun copy(): Foo {  
return Foo(a, b)  
}  
}

data class Bar(val c: Int)
