相关疑难解决方法(0)

打字稿 - 克隆对象

我有一个超类是父(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 …

javascript typescript

131
推荐指数
11
解决办法
22万
查看次数

标签 统计

javascript ×1

typescript ×1