Cloning Objects in JavaScript

Zsolt Nagy
6 min readJul 25, 2015

This article was originally posted on zsoltnagy.eu. Even though the post is from 2015, this was the most popular article on my tech blog in 2017. For this reason, in order to reach a wider audience, I am sharing this post with you on Medium. If you are interested in reading the original post with JavaScript syntax highlighting, check out my original post here.

JavaScript made its way from being a toy language for simple animations to becoming a language for client side and server side web application development. Some generic concepts also made their way to the JavaScript world and developers become more and more aware of them. Cloning objects is one of these concepts.

Cloning can be quite complex. Prototypal inheritance, reference types and methods associated with an object may require a specialized approach. Restrictions on cloned data may simplify cloning. It is the responsibility of the developers to understand and apply the correct cloning method on a case by case basis.

Shallow Copy

Cloning methods of most libraries are implemented using shallow copying. One example is _.clone, the clone method of UnderscoreJs.

Shallow Copy: all field keys and values of the original object are copied to the new object.

--

--

Zsolt Nagy

I am a web development team lead, mentor, and software engineer living in Berlin, Germany authoring two blogs: zsoltnagy.eu and devcareermastery.com.