我有一个超类是父(Entity)对于很多子类(Customer,Product,ProductCategory…)
Entity
Customer
Product
ProductCategory
我正在寻找动态克隆在Typescript中包含不同子对象的对象。
例如:一个Customer具有不同特征的Product人具有一个ProductCategory
var cust:Customer = new Customer (); cust.name = "someName"; cust.products.push(new Product(someId1)); cust.products.push(new Product(someId2));
为了克隆整个对象树,我在其中创建了一个函数 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; }
在new上升时,它被transpiled为JavaScript以下错误:error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
new
error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
尽管该脚本有效,但 我想摆脱已编译的错误
解决具体问题
您可以使用类型断言来告诉编译器您更了解:
public clone(): any { var cloneObj = new (<any>this.constructor()); for (var attribut in this) { if (typeof this[attribut] === "object") { cloneObj[attribut] = this[attribut].clone(); } else { cloneObj[attribut] = this[attribut]; } } return cloneObj; }
克隆
请记住,有时最好编写自己的映射-而不是完全动态。但是,您可以使用一些“克隆”技巧来给您带来不同的效果。
我将在下面的所有示例中使用以下代码:
class Example { constructor(public type: string) { } } class Customer { constructor(public name: string, public example: Example) { } greet() { return 'Hello ' + this.name; } } var customer = new Customer('David', new Example('DavidType'));
选项1:点差
属性: 是 方法:否 深度复制:否
var clone = { ...customer }; alert(clone.name + ' ' + clone.example.type); // David DavidType //alert(clone.greet()); // Not OK clone.name = 'Steve'; clone.example.type = 'SteveType'; alert(customer.name + ' ' + customer.example.type); // David SteveType
选项2:Object.assign
var clone = Object.assign({}, customer); alert(clone.name + ' ' + clone.example.type); // David DavidType alert(clone.greet()); // Not OK, although compiler won't spot it clone.name = 'Steve'; clone.example.type = 'SteveType'; alert(customer.name + ' ' + customer.example.type); // David SteveType
选项3:Object.create
属性: 继承的 方法: 继承的 深复制: 浅的继承 (深的更改会影响原始副本和克隆副本)
var clone = Object.create(customer); alert(clone.name + ' ' + clone.example.type); // David DavidType alert(clone.greet()); // OK customer.name = 'Misha'; customer.example = new Example("MishaType"); // clone sees changes to original alert(clone.name + ' ' + clone.example.type); // Misha MishaType clone.name = 'Steve'; clone.example.type = 'SteveType'; // original sees changes to clone alert(customer.name + ' ' + customer.example.type); // Misha SteveType
选项4:深层复制功能
属性: 是 方法:否 深度复制: 是
function deepCopy(obj) { var copy; // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (var i = 0, len = obj.length; i < len; i++) { copy[i] = deepCopy(obj[i]); } return copy; } // Handle Object if (obj instanceof Object) { copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = deepCopy(obj[attr]); } return copy; } throw new Error("Unable to copy obj! Its type isn't supported."); } var clone = <Customer>deepCopy(customer); alert(clone.name + ' ' + clone.example.type); // David DavidType // alert(clone.greet()); // Not OK - not really a customer clone.name = 'Steve'; clone.example.type = 'SteveType'; alert(customer.name + ' ' + customer.example.type); // David DavidType