📜  在打字稿代码示例中复制对象

📅  最后修改于: 2022-03-11 14:48:20.131000             🧑  作者: Mango

代码示例1
//1.Shallow copy:
let Copy = {...yourObject}
//2.Deep Copy: a. through recusive typing functionality:
let Cone = DeepCopy(yourObject);
public DeepCopy(object: any): any
{
  if(object === null)
  {
    return null;
  }
  const returnObj = {};
  Object.entries(object).forEach(
  ([key, value]) =>{
    const objType = typeof value
  if(objType !== "object" || value === null){
     returnObj[key] = value;
  }
  else{
      returnObj[key] = DeepCopy(value);
  }
}
//b.Hardway: repeat the following expanstions for all complex types as deep as you need
let Copy = {...yourObject, yourObjsComplexProp: {...yourObject.yourObjsComplexProp}}