473,461 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Deep Cloning JS-Objects

gits
5,390 Expert Mod 4TB
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 13270
Dormilich
8,658 Expert Mod 8TB
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 Expert Mod 4TB
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 Expert Mod 8TB
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 Expert Mod 4TB
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 Expert Mod 8TB
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 Expert Mod 8TB
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 Expert Mod 4TB
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.hasOwnProperty(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 Expert Mod 8TB
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–sometimes 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 Expert Mod 8TB
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
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...
2
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
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...
1
by: Neven Klofutar | last post by:
Hi, Can please someone send me a link or an example of deep cloning. thanx, Neven
5
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...
22
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 =...
3
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...
1
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...
8
by: scf1984 | last post by:
How does one perform a deep clone (by value, not by reference) to an object in JavaScript?
3
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.