+ 1

What is Shallow cloning or merging objects

In ES6

19th May 2020, 6:57 PM
Abhay
Abhay - avatar
2 odpowiedzi
+ 2
Shallow cloning means the variables value is stored in the same location. Merging objects means to make two objects a single object using merge() method
19th May 2020, 7:20 PM
Sajid
Sajid - avatar
+ 2
An example: let a = { info: { name: "joe" } } now let's copy `a`, by making a new object that contains a.x: let b = { info: a.info } // or alternatively let b = { ...a } That's a shallow copy. What does that mean? Consider b.info.name = "mary"; What happened to `a`? console.log(a); >>> { info: { name: "mary" } } So changing b.info.name changed a.info.name. That's because a.info and b.info point to the same object. But check this: b.info = "not available" console.log(a); >>> { info: { name: "mary" } } So that did not change a.info because we pointed b.info to another object. And that's it, when you do something like b = { ...a } in ES6, it copies the object, but only "one layer deep". Try it out in the code playground to wrap your head around it. I see you did the C++ course, if you know about pointers this should make a lot more sense.
19th May 2020, 9:09 PM
Schindlabua
Schindlabua - avatar