如何克隆JavaScript类实例?
我尝试了普通的jQuery扩展,但这只是返回一个vanilla对象.我在堆栈上查看了许多其他答案,但找不到如何克隆实例.
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);
Run Code Online (Sandbox Code Playgroud)
我希望克隆是深度/递归的.IE也克隆了作为对象的所有属性.