How do I clone a JavaScript class instance?
I tried the normal jQuery extend, but that just returns a vanilla object. I have looked through many other answers on stack, but could not find how to clone an instance.
function Parent(name) {
this.name = name;
}
Parent.prototype.sayHello = function() {
console.log('Hello my name is ' + this.name);
}
function Child(name) {
Parent.call(this, name);
}
Child.prototype = Object.create(Parent.prototype);
var child = new Child('Billy');
var clone = $.extend(true, {}, child);
clone.name = 'Bob';
child.sayHello();
clone.sayHello();
console.log(child instanceof Child);
console.log(clone instanceof Child);
I would prefer that the clone was deep/recursive. I.E. all properties that are objects are cloned as well.