我有一个超类是父(Entity)对于很多子类(Customer,Product,ProductCategory...)
我正在寻找动态克隆在Typescript中包含不同子对象的对象.
例如:a Customer有不同的Product人有ProductCategory
var cust:Customer = new Customer ();
cust.name = "someName";
cust.products.push(new Product(someId1));
cust.products.push(new Product(someId2));
Run Code Online (Sandbox Code Playgroud)
为了克隆整个对象树,我创建了一个函数 Entity
public clone():any {
var cloneObj = new this.constructor();
for (var attribut in this) {
if(typeof this[attribut] === "object"){
cloneObj[attribut] = this.clone();
} else {
cloneObj[attribut] = this[attribut];
}
}
return cloneObj;
}
Run Code Online (Sandbox Code Playgroud)
在new上升时,它被transpiled为JavaScript以下错误:error TS2351: Cannot use 'new' with an expression whose type lacks a call or …