shallow copy and deep copy in javascript

copy means copy from something but original and copy both are same


deep copy -> if copy original removed or disconencted

const a =5;

b=a;

b=6;

console.log(b)

6

shallow copy

parent and copied both are tehre


declaraion based on that defining copy

const original = { name: { value: "John Doe" } }; const shallowCopy = { ...original }; const deepCopy = JSON.parse(JSON.stringify(original)); shallowCopy.name.value = "Jane Doe"; console.log(original.name.value); // "John Doe" console.log(deepCopy.name.value); // "Jane Doe"

combining two objects or copy object to const


const original = { name: "John Doe", age: 30, address: { city: "New York", state: "NY" } }; const shallowCopy = Object.assign({}, original); shallowCopy.name = "Jane Doe"; console.log(original.name); // "Jane Doe"


assing and spread operator - removes parent one and it comes under shallow copy

onst shallowCopy = Object.assign({}, original);


const shallowCopy = { ...original };


No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...