I'd like to clone some objects deep. I've seen this question:
Creating a copy of an object in Javascript
... However, none of the answers offers a way to clone a function deeply. In other words, if an array object has functions, its clones will have references to that function.
I'll give you an example. With the following object as a starting point:
var cores = {
Amarelo: function () {}
}
cores.getAmarelo.prototype.r = 255;
cores.getAmarelo.prototype.g = 255;
cores.getAmarelo.prototype.b = 0;
If I do a shallow clone, the clone will have the same Amarelo
function, by reference ... And so changing the prototype of the function on an object will also affect its clone. I.e.:
var paletaDaltonica = {};
for (var cor in cores) {
paletaDaltonica[cor] = cores[cor];
}
paletaDaltonica.Amarelo.prototype.r = 120;
// Isso alterou o protótipo de Amarelo no objeto original também.
Is it possible to clone a function in a deep way, so that the clone has the same functionality and prototype of the original function, but keeping the changes in their respective prototypes isolated?