[thelist] JS Object Cloning
Joshua OIson
joshua at alphashop.net
Tue Mar 27 16:06:21 CST 2001
Thank you.
The code I ended up finding looked like this:
function cloneObject(what) {
for (i in what) {
if (typeof what[i] == 'object') {
this[i] = new cloneObject(what[i]);
}
else
this[i] = what[i];
}
}
var new_object = new cloneObject(old_object);
Found this code on http://developer.irt.org/script/object.htm
It recurses through and seems to clone the entire object, and it's
subobjects. I was very close to this code in my own recursive function, but
I did not know the typeof operator, which was the final piece to the puzzle.
Thanks again.
-joshua
----- Original Message -----
From: "Erik" <gozz at gozz.com>
Subject: Re: [thelist] JS Object Cloning
> No clone() function I know of, but you can do something like this:
>
> var originalArray = new Array('a','b','c');
>
> function Clone(original) {
> for (i in original) {
> this[i] = original[i];
> }
> }
>
> var clonedArray = new Clone(originalArray);
More information about the thelist
mailing list