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?
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?