Exercises: Array and Object Spread Syntax - Javascript In Depth

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 10

  • @simplyskandi5973
    @simplyskandi5973 2 ปีที่แล้ว +1

    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!

    • @TechWithNader
      @TechWithNader  2 ปีที่แล้ว

      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 😄

  • @khanittaajay3932
    @khanittaajay3932 2 ปีที่แล้ว +1

    Hello Nader! Great exercises! Thank you!

    • @TechWithNader
      @TechWithNader  2 ปีที่แล้ว

      Thank you very much! Glad you’re enjoying them and learning 😊

  • @Abulkhair1995
    @Abulkhair1995 ปีที่แล้ว +2

    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);

  • @KRAKENBACK..
    @KRAKENBACK.. ปีที่แล้ว +2

    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 😂😂

    • @TechWithNader
      @TechWithNader  ปีที่แล้ว +1

      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!

    • @KRAKENBACK..
      @KRAKENBACK.. ปีที่แล้ว +1

      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?

    • @TechWithNader
      @TechWithNader  ปีที่แล้ว +2

      @@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)