大佬教程收集整理的这篇文章主要介绍了typescript - cloning object,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
解决具体问题
您可以使用类型断言来告诉编译器您更了解:
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
@H_450_0@解决方法
我有一个超类是父(Entity
)对于很多子类(Customer
,Product
,ProductCategory
&Hellip;)
我正在寻找动态克隆在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.
尽管该脚本有效,但 我想摆脱已编译的错误
以上是大佬教程为你收集整理的typescript - cloning object全部内容,希望文章能够帮你解决typescript - cloning object所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。