[jQuery] $.clone
[jQuery] $.clone
During assingment, if the object is not primative, Javascript will
return a pointer to the object rather than a copy.
E.g.
a = [1,2]
b = a
b[0]=3
a ==> [3,2]
This clone function makes it possible to copy an object.
$.clone = function (obj) {
if(typeof(obj) != 'object') return obj;
if(obj == null) return obj;
var newobj = new Object();
for(var i in obj)
newobj[i] = $.clone(obj[i]);
return newobj;
}
a = [1,2]
b = $.clone(a)
b[0]=3
a ==> [1,2]
I have found this function invaluable when comparing and complex
Javascript objects.