I hope you'll help me.
I want to make a deep copy of this object User.
function clone(obj) {
if (null == obj || "object" != typeof obj)
return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr))
copy[attr] = obj[attr];
}
return copy;
}
var User = {
_firstName: 'robert',
get firstName() {
return this._firstName;
},
set firstName(val) {
this._firstName = val;
},
affFirstName: function() {
console.log(this._firstName);
},
};
console.log(User);
var User2 = clone(User);
console.log(User2);
This function clone doesn't work (see fiddle for example)
This copy creates new variable with the name of get and set.
Set and get Operators are not copied.
But the function affFirstName() is well copied.
Do you have a solution ?