在Javascript中克隆扩展对象,保留所有父对象';s方法

在Javascript中克隆扩展对象,保留所有父对象';s方法,javascript,ecmascript-6,clone,es6-class,Javascript,Ecmascript 6,Clone,Es6 Class,我无法在JS中实现一个完整且令人满意的克隆方法。 我有一个父类,有属性和方法,当然每个派生类都必须访问父类的方法 班长{ 构造函数(){ 这个.fatherProp=1; } 法{ log(“父方法”); } } 还有这个子类,它扩展了前一个 类子类扩展父类{ 构造函数(){ 超级(); this.childProp=2; } } 客户端代码运行良好 let child1=new Child(); console.log(child1);//控制台:Child{fatherProp:1,ch

我无法在JS中实现一个完整且令人满意的克隆方法。 我有一个父类,有属性和方法,当然每个派生类都必须访问父类的方法

班长{
构造函数(){
这个.fatherProp=1;
}
法{
log(“父方法”);
}
}
还有这个子类,它扩展了前一个

类子类扩展父类{
构造函数(){
超级();
this.childProp=2;
}
}
客户端代码运行良好

let child1=new Child();
console.log(child1);//控制台:Child{fatherProp:1,childProp:2}
child1.fatherMethod();//控制台:父方法
然后,我需要克隆子对象,当然要保留所有相同的父/子类结构、属性和方法。所以我在父类中添加了一个克隆方法

班长{
构造函数(){
这个.fatherProp=1;
}
法{
log(“父方法”);
}
克隆(){
让newObject={};
Object.assign(newObject,this);
返回newObject;
}
}
客户机代码工作得很好

let child2 = child1.clone();
console.log(child2); // CONSOLE: {fatherProp: 1, childProp: 2} *** "Child" type missing
child2.fatherMethod(); // CONSOLE: Uncaught TypeError: child2.fatherMethod is not a function
深入记录这两个对象,我可以看到第一个孩子(图中的蓝色)将“父亲”作为“原型”。而第二个对象(红色)的proto为空

发生什么事了? 在这种情况下,我应该如何克隆对象?
谢谢

克隆方法返回对象而不是类,单向:

班长{
构造函数(){
这个.fatherProp=1;
}
法{
log(“父方法”);
}
克隆(){
让clone=Object.assign(Object.create(Object.getPrototypeOf(this)),this);
返回克隆;
}
}
班上的孩子比父亲长{
构造函数(){
超级();
this.childProp=2;
}
}
让child1=新的Child();
console.log(child1);//控制台:Child{fatherProp:1,childProp:2}
child1.fatherMethod();//控制台:父方法
让child2=child1.clone();
console.log(child2);//控制台:{fatherProp:1,childProp:2}***缺少“Child”类型

child2.fatherMethod();//CONSOLE:uncaughttypeerror:child2.fatherMethod不是一个函数
这是
newObject={}
创建的。您需要创建一个类的实例。感谢大家,它可以正常工作。(这实际上是一个重复的问题。)