How do I clone a Javascript class instance using ES6.
I\'m not interested in solutions based on jquery or $extend.
I\'ve seen quite old discussions of object
I like almost all the answers. I had this problem and to resolve it I would do it manually by defining a clone()
method and inside it, I would build the whole object from scratch. For me, this makes sense because the resulted object will be naturally of the same type as the cloned object.
Example with typescript:
export default class ClassName {
private name: string;
private anotherVariable: string;
constructor(name: string, anotherVariable: string) {
this.name = name;
this.anotherVariable = anotherVariable;
}
public clone(): ClassName {
return new ClassName(this.name, this.anotherVariable);
}
}
I like this solution because it looks more 'Object Oriented'y