I have an entity class as fallow:
export class Contact {
id: number;
firstName: string;
lastName: string;
constructor(id?, firstName?, lastName?) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
}
and want to have a different copy from Contact:
export class MyComponent {
contactSrc = new Contact();
contactDest = new Contact();
constructor() {
this.contactSrc.firstName = "Adam"
}
handleCopyOfObject() {
console.log("firstName for contactSrc: " + this.contactSrc.firstName);
this.contactDest = this.contactSrc;
this.contactDest.firstName = "Mohsen";
console.log("-----------result after copy -----------");
console.log("firstName for contactSrc: " + this.contactSrc.firstName);
console.log("firstName for contactDest: " + this.contactDest.firstName);
}
}
the output is:
firstName for contactSrc: Adam
-----------result after copy -----------
firstName for contactSrc: Mohsen
firstName for contactDest: Mohsen
you can see that by changing contactDest.firstName, contactSrc.firstName also changes. how can I change contactDest.firstName without effecting the contactSrc.firstName?