1

I have object in typescript:

export class A {
 public var1: boolean = false;
 public var2: boolean = false;
 public b: B = new B();

}

export class B {
   public v: boolean = false;
}

And declare and exist object:

...
public a: A = new A();
...

When I use jQuery:

const myCopy: A = $.extend(true, {}, this.a);
this.a.b.v = true;

When I change variable object this.a I see change in copy. How Can I deep copy in this situation.

Stack Over
  • 287
  • 2
  • 4
  • 14

1 Answers1

5

You can use triple ...

let b = new A();
const mCopy = {...b}; 

or you can use json stringify and json parse

const mCopy = JSON.parse(JSON.stringify(target));