Tuesday, April 10, 2012

Duplicate a javascript prototype instance?

I'm given an instance "aNode" of a prototype called "Node". Following this discussion (http://stackoverflow.com/questions/728360/copying-an-object-in-javascript), I attempt to:



var newNode = clone(aNode);


The clone function goes like:



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;
}


Despite obj.consturctor() properly refers to aNode's constructor, copy is "underfined". Can't find why :(



Is this method wrong, or am I missing something :) ?



Thanks,
J.





No comments:

Post a Comment