Exercise-03 really helped me understand the concept of shallow copy vs actual copy. I have always found this concept super confusing and sort of glazed over it. Thank you for this brilliantly thought-through qs!
Thanks! I’m glad it was helpful! It’s a really tricky concept and will bite us really hard in subtle ways as we build more complex program and deal with things like state management later 😄
While doing exercise 3 the way I did it was completley wrong LOL I thought it was asking us to create another copy but with only enemies and best movies, So i did / let enemies = superhero.enemies; // let bestMovie = superhero.bestMovie; // // console.log(enemies); // // console.log(bestMovie); // const superheroCopy = { enemies: enemies, bestMovie: bestMovie }; // console.log(superheroCopy); // superheroCopy.enemies.push("Poison Ivy"); // superheroCopy.bestMovie.year = 2008; But then watching the solution I totally understand how we were able to create a copy of the whole object and overwrite some of the properties to add different primitives and not change the original object. I was wondering why when solving the problem that this felt more of like a .push exercise and now I realize I was doing something entirely different then the question was asking 😂😂
Haha all good! Some of these instructions are extra long and I could probably find a way to make them less confusing 😂 Glad you see how to do it both ways though!
is the reason the shallow copy gets to alter its properties because we are adding/changing a primitive? Would it have changed the original if we were adding/changing a non primitive that could've been inside of the object?
@@KRAKENBACK.. Yeah generally primitives are always copies no matter what or where they are. Everything else is always a reference, so changing it in one spot will change it elsewhere and we don't copy when moving/referencing them in different spots (including spread)
Exercise-03 really helped me understand the concept of shallow copy vs actual copy. I have always found this concept super confusing and sort of glazed over it. Thank you for this brilliantly thought-through qs!
Thanks! I’m glad it was helpful! It’s a really tricky concept and will bite us really hard in subtle ways as we build more complex program and deal with things like state management later 😄
Hello Nader! Great exercises! Thank you!
Thank you very much! Glad you’re enjoying them and learning 😊
const superhero = {
name:"Bruce Wayne",allies:"Batman",Universe: "DC",Happy:false,enmies:["Catwoman","Joker","Sucker"],
bestMOvies:{title:"Dark Knight",ranting:95}
}
const copy = {
...superhero
}
const copy1 = {
...superhero.enmies,3:"Poison ivy"
}
const copy2 = {
...superhero.bestMOvies,
year:200000
}
copy2.year=2008;
console.log(superhero);
console.log(copy);
While doing exercise 3 the way I did it was completley wrong LOL I thought it was asking us to create another copy but with only enemies and best movies, So i did / let enemies = superhero.enemies;
// let bestMovie = superhero.bestMovie;
// // console.log(enemies);
// // console.log(bestMovie);
// const superheroCopy = { enemies: enemies, bestMovie: bestMovie };
// console.log(superheroCopy);
// superheroCopy.enemies.push("Poison Ivy");
// superheroCopy.bestMovie.year = 2008;
But then watching the solution I totally understand how we were able to create a copy of the whole object and overwrite some of the properties to add different primitives and not change the original object. I was wondering why when solving the problem that this felt more of like a .push exercise and now I realize I was doing something entirely different then the question was asking 😂😂
Haha all good! Some of these instructions are extra long and I could probably find a way to make them less confusing 😂 Glad you see how to do it both ways though!
is the reason the shallow copy gets to alter its properties because we are adding/changing a primitive? Would it have changed the original if we were adding/changing a non primitive that could've been inside of the object?
@@KRAKENBACK.. Yeah generally primitives are always copies no matter what or where they are. Everything else is always a reference, so changing it in one spot will change it elsewhere and we don't copy when moving/referencing them in different spots (including spread)