I've written my own deepCopy-Function, that is able to copy and Object:
function deepCopyObj(object){
if(object == null || typeof(object) != 'object'){
return object;
}
var copy = object.constructor(); //This line makes some troubles
for (var attr in object) {
if(object.hasOwnProperty(attr) && typeof(object[attr]) !== "undefined") {
copy[attr] = deepCopyObj(object[attr]);
}
}
return copy;
}
This code always worked fine - until now:
Sometimes, when I want to copy an object, the command var copy = object.constructor();
returns undefined
.
What is the reason for that? When I print object
to the console, it gives me the correct output.
Notice, that my code sometimes use delete object.anAttribute;
to remove functions - but I don't know if this can be the reason.