Skip to content

How to Clone Object in JavaScript

What is Best Way to Clone Object in JavaScript. JavaScript - Best Way to Clone Object. cloning object uisng jquery extend function in javascript.

On this page

We found jQuery’s extend() function is the best way for creating a clone object in JavaScript. jQuery’s extend() functions is also useful for merging two or more objects in a single object.

This article will help you to create a clone of an object in JavaScript using jQuery’s extend() function.

JavaScript Clone Object Code:

If you look in below code in JavaScript, you will find that first we have defined our first object named Obj1 with some demo values. After that we have cloned it to new object named Obj2 using extend() function. Then updated value of Obj2.name variable for making some difference for understanding.

Example of Object Clone using jQuery’s extend():

First create a test.html file using following code on your system. We have used above JavaScript code for creating clone of object.

Now access this page in web browser and check console logs. For this example I am using Firebug on Firefox browser to view console logs and found following results.

jquery-extend-example

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top