I want to create a copy of a JavaScript object which has enumerable and non-enumerable properties. I want make a exact replica of the object with all enumerable and non-enumerable properties copied to the new one.
Any help how can it be done?
I want to create a copy of a JavaScript object which has enumerable and non-enumerable properties. I want make a exact replica of the object with all enumerable and non-enumerable properties copied to the new one.
Any help how can it be done?
JSON.parse(JSON.stringify(obj))
You should use Object.create
or it's backward compatible counterpart.
if(!Object.create){
Object.create = function(o){
function F(){}; F.prototype = o;
return new F;
}
}
var oldObj = {prop1:'val1', prop2:'val2', prop3:'val3'};
var newObj = Object.create(oldObj);
delete newObj.prop2;
console.log(newObj); console.log(oldObj);