what is best way to write a clone method?

R

Richard Trahan

I want to write a clone method for an object that has one or more
ancestors (prototypes). I can't just define the ancestors' data elements
as properties of the new object, because that would ruin the prototype
scheme.

Is there an idiomatic way to do this? It seems that I would have to
clone each ancestor object, and set the prototype member of each. In
pseudo code:
var newchild = new ChildObjectTemplate();
newchild.prototype = new ParentObjectTemplate();
newchild.prototype.data = clone(oldchild.prototype.data);
newchild.data = clone(oldchild.data);

In a regular class-oriented language like C++ or Java, I would just have
to write a clone method for each class. Is there something equivalent in js?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top