Is this an okay clone function for cloning an object recursively?
function clone(o)
{
function CloneObject(inObj)
{
for (i in inObj)
{
if(typeof inObj[i] == 'object')
this[i] = clone(inObj[i]);
else
this[i] = inObj[i];
}
}
return new CloneObject(o);
}
Also, i found out this doesn’t work with arrays. How can I clone an array?
It certainly doesn’t clone the object perfectly — the clone won’t have the original’s prototype, and they’ll have different constructors, and if the original has any non-iterable properties, then this won’t copy them — but you ask if it’s “okay”, and the answer to that may well be “yes”: If what it does is all that you need it to do, then it’s absolutely fine.
As for cloning arrays — you could check if
inObj.constructor == Array.