Java Reference
In-Depth Information
To do this, we'll change our code so that it checks to see if the property being copied is an
object. If it is, the mixin method is called again on that property:
Object.defineProperty(Object.prototype, 'mixin', {
enumerable: false,
writable: false,
configurable: false,
value: function() {
for (var i = 0, max = arguments.length ; i < max ; i++)
{
if(typeof arguments[i] === "object") {
var object = arguments[i];
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] === "object") {
this[property] = (object[property].constructor
=== Array)
? [] : {};
this[property].mixin(object[property]);
} else {
var description =
Object.getOwnPropertyDescriptor(object,
property);
Object.defineProperty(this,property,
description);
}
}
}
}
}
return this;
}
});
Let's test this to see if it makes a deep copy:
 
Search WWH ::




Custom Search