473,608 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep Cloning JS-Objects

gits
5,390 Recognized Expert Moderator Expert
This short article introduces a method that may be used to create a 'deep-copy' of an javascript object. You might ask: 'Wherefore do we need this?' ... Answer: 'Only variable-values of the basic data-types string, int, float, boolean and to make the confusion perfect :) functions too! are passed by value, all others are passed by reference.' This means, when you create an object a, like in the code below, and you assign object a to variable b then you will pass the object a by reference. That results in the fact, that when you assign a new value to b.foobar, a.foobar will be changed accordingly. To pass it by value, what you can imagine as a (deep-)copy of object a, we have to use a function that recursivly copies the values of any datastructure inside of object a to object b.

Expand|Select|Wrap|Line Numbers
  1. // example object that might be cloned
  2. var a = {
  3.    foo   : { test: 1, test1: 2 },
  4.    bar   : function(a) { alert(a) },
  5.    foobar: 'foobar1',
  6.    arr   : [1, [8, 9], {1: 1, 2: 2}, 4]
  7. };
  8.  
  9. /**
  10.  * function clone_obj deep-clones an js-object
  11.  *
  12.  * @param obj javascript-object
  13.  * @return c cloned javascript-object
  14.  */
  15. function clone_obj(obj) {
  16.     if (typeof obj !== 'object' || obj == null) {
  17.         return obj;
  18.     }
  19.  
  20.     var c = obj instanceof Array ? [] : {};
  21.  
  22.     for (var i in obj) {
  23.         var prop = obj[i];
  24.  
  25.         if (typeof prop == 'object') {
  26.            if (prop instanceof Array) {
  27.                c[i] = [];
  28.  
  29.                for (var j = 0; j < prop.length; j++) {
  30.                    if (typeof prop[j] != 'object') {
  31.                        c[i].push(prop[j]);
  32.                    } else {
  33.                        c[i].push(clone_obj(prop[j]));
  34.                    }
  35.                }
  36.            } else {
  37.                c[i] = clone_obj(prop);
  38.            }
  39.         } else {
  40.            c[i] = prop;
  41.         }
  42.     }
  43.  
  44.     return c;
  45. }
  46.  
  47. // usage: we clone object a to object b
  48. var b = clone_obj(a);
  49.  
Note: Why can't we copy dom-nodes with that function? Answer: dom-nodes contain references to its parent- and child-objects etc. When copying this with the above method we will get an error: 'too much recursion' ... because when copying a parent object we come to a child of that and from there to its parent and so on ... for cloning dom-nodes we simply may use the cloneNode() dom-method.

testers needed ;) ... i think it works the way it should ... but may be I've overlooked something?

testing-help: when testing in firebug console the test scenario could be for example:

Expand|Select|Wrap|Line Numbers
  1. var a = {
  2.    foo   : { test: 1, test1: 2 },
  3.    bar   : function(a) { alert(a) },
  4.    foobar: 'foobar1',
  5.    arr   : [1, [8, 9], {1: 1, 2: 2}, 4]
  6. };
  7.  
  8. var c = a;
  9. c.foobar = 'new test';
  10.  
  11. function clone_obj(obj) {
  12.     if (typeof obj !== 'object' || obj == null) {
  13.         return obj;
  14.     }
  15.  
  16.     var c = obj instanceof Array ? [] : {};
  17.  
  18.     for (var i in obj) {
  19.         var prop = obj[i];
  20.  
  21.         if (typeof prop == 'object') {
  22.            if (prop instanceof Array) {
  23.                c[i] = [];
  24.  
  25.                for (var j = 0; j < prop.length; j++) {
  26.                    if (typeof prop[j] != 'object') {
  27.                        c[i].push(prop[j]);
  28.                    } else {
  29.                        c[i].push(clone_obj(prop[j]));
  30.                    }
  31.                }
  32.            } else {
  33.                c[i] = clone_obj(prop);
  34.            }
  35.         } else {
  36.            c[i] = prop;
  37.         }
  38.     }
  39.  
  40.     return c;
  41. }
  42.  
  43. var b = clone_obj(a);
  44. b.foobar = 'test';
  45. b.bar = function(a) { alert(a + a) };
  46.  
  47. console.info(a.toSource());
  48. console.info(b.toSource());
  49.  
Oct 4 '07 #1
9 13291
Dormilich
8,658 Recognized Expert Moderator Expert
the sub-loops are unnecessary. clone_obj() takes care of the data types itself
Expand|Select|Wrap|Line Numbers
  1. function clone_obj(obj) {
  2.     if (typeof obj !== 'object' || obj === null) {
  3.         return obj;
  4.     }
  5.  
  6.     var c = obj instanceof Array ? [] : {};
  7.  
  8.     for (var i in obj) {
  9.         if (obj.hasOwnProperty(i)) {
  10.             c[i] = clone_obj(obj[i]);
  11.         }
  12.     }
  13.  
  14.     return c;
  15. }
Mar 14 '10 #2
gits
5,390 Recognized Expert Moderator Expert
seems reasonable and the method looks much better now :) ... thanks ... it was a kind of one step after another method before and now it seems quite well refactored to a ready one :)

kind regards,
gits
Mar 14 '10 #3
Dormilich
8,658 Recognized Expert Moderator Expert
the remaining problems of the functions I see:
- besides the literals, there are only Object and Array supported as type (thus Boolean, String, Number, RegExp, Error, etc. will be cloned as Object)
- custom prototypes are cloned as well
- custom "classes" are cloned as Object
Mar 15 '10 #4
gits
5,390 Recognized Expert Moderator Expert
but that should be sufficient ... since only arrays and objects needs to be recursivly forced to be 'copied' by value ... the outcome should only be a 'deep-copy' of the original object?
Mar 15 '10 #5
Dormilich
8,658 Recognized Expert Moderator Expert
I understand that Boolean, String and Number objects can be passed as literal, but the others would just loose their built-in methods. and a custom object would be copied with all its methods and loses its constructor, quite some overhead, if you ask me.
Expand|Select|Wrap|Line Numbers
  1. function Foo()
  2. {
  3.     this.bar = new Boolean(1);
  4. }
  5.  
  6. Foo.prototype.foobar = function (x) { alert(x); };
  7.  
  8. var x = new Foo();
  9. var y = clone_obj(x);
  10. console.info(y instanceof Foo); // false
  11. y.foobar(2); // foobar() is not a function
  12.  
Mar 15 '10 #6
Dormilich
8,658 Recognized Expert Moderator Expert
preserves the object constructor (and doesn’t clone DOM objects)
Expand|Select|Wrap|Line Numbers
  1. function clone_obj(obj) 
  2. {
  3.     if (typeof obj !== 'object' || obj === null) {
  4.         return obj;
  5.     }
  6.     if (obj instanceof Node || obj instanceof NodeList || obj instanceof NamedNodeMap) {
  7.         return obj;
  8.     }
  9.  
  10.     var a = obj.valueOf ? obj.valueOf() : undefined;
  11.     var c = new window[obj.constructor.name](a);
  12.  
  13.     for (var i in obj) {
  14.         if (obj.hasOwnProperty(i)) {
  15.             c[i] = clone_obj(obj[i]);
  16.         }
  17.     }
  18.  
  19.     return c;
  20. }
Mar 15 '10 #7
gits
5,390 Recognized Expert Moderator Expert
didn't find time yet to have a closer look ... but the prototype must be copied ... since i would expect that when cloning an object (with the orig function it does - in your above test foobar wasn't copied) -> so i would say that obj.hasOwnPrope rty(i) could have been removed before cloning a property ... the constructor is an issue and should be fixed ... in case you would need to ask that question anytime ... :) ... the new operation would create a new obj instance and then the above condition should be back in there :) ... but i just suspect that using obj.constructor .name isn't cross browser safe ... since i think there is a problem in IE with that?
Mar 15 '10 #8
Dormilich
8,658 Recognized Expert Moderator Expert
IMO, as long as you have the correct constructor, there is no need to copy the prototype at all, or on the other hand side, copy the prototype itself, which should lift off much of the recursion (would that be the way to go for extended objects?).

and another obstacle are closures–someti mes I use them to create immutable properties–and private variables. I don’t know, whether they can be copied at all.

yea, I don’t like the obj.constructor .name that much either, but what else is there to do? (can’t tell anything about IE, due to a lack of it)
Mar 15 '10 #9
Dormilich
8,658 Recognized Expert Moderator Expert
found some clone code … don’t know how good it is (yet)
Expand|Select|Wrap|Line Numbers
  1. function clone(obj) {
  2.   function Constructor(){}
  3.   Constructor.prototype = obj;
  4.   return new Constructor();
  5. }
[edit]not very good …[/edit]
Mar 26 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2709
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the behavior/events on the original rows image. I overwrote the onclick as well as attached a behavior to the image in cloned row, but events such as onmouseover execute now on both images when the duplicate is mouse overed.
2
1752
by: Hendrik Schober | last post by:
Hi, I need something like this: class X { private: struct Impl_ { virtual ~Impl_() {} virtual Impl_* clone() const = 0; };
3
1718
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when should use cloning????//
1
1492
by: Neven Klofutar | last post by:
Hi, Can please someone send me a link or an example of deep cloning. thanx, Neven
5
25638
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's Collection classes does not support deep copy methods by theself, only Clone with shallow copy.
22
15923
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b = (ArrayList) a.Clone();
3
8744
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
1
1776
by: Dan Dorey | last post by:
I've implemented the ICloneable interface on one of my class. I've written this simple code in two different ways and I think both should work but it's not the case and I'm curious to understand why. The working method: CallflowBase ret = null; ret = this.MemberwiseClone() as CallflowBase; ret.m_callflowName = this.m_callflowName;
8
7406
by: scf1984 | last post by:
How does one perform a deep clone (by value, not by reference) to an object in JavaScript?
3
2073
by: gasfusion | last post by:
Hey guys. I have a little problem trying to clone my object, which is shown below. If you notice below, i am using a generic linked list structure to store byte arrays. The problem is when i use my Clone() method to clone this object, it doesn't clone the actual LinkedList. Any ideas on how to go about fixing this? Much appreciated. using System; using System.Reflection; using System.Collections.Generic; namespace Compiler { ...
0
8063
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6817
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.