Shallow Copy and Deep Copy | Advanced Javascript Tutorial

แชร์
ฝัง

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

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  3 ปีที่แล้ว +4

    Knowing the difference between shallow copies and deep copies of structural data is foundational knowledge for coding pure functions. In this advanced Javascript tutorial, you will learn about primitive vs structural data, passing values vs passing references, mutable vs immutable data, and how to make both shallow and deep copies of structural data. If you are just starting to learn Javascript, I recommend starting with my 8 hour full course tutorial here: th-cam.com/video/EfAl9bwzVZk/w-d-xo.html

  • @prudhvichinnam1488
    @prudhvichinnam1488 3 ปีที่แล้ว +10

    Why these guy didn't get views please support him we love davegray

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

    Dave, you shared so much important information here! Thank you so much!!! This is essential to pass any technical interview and I'm super thankful for this video!

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

    Dave, you are an amazing teacher. I have built an entire ePOS system on my own while studying how to program. Your tutorials on Node and express helped me a lot on building my backend server.
    Your teaching are on point and easy to understand. I’m buying you a coffee. Thank you brother

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

      Thank you for the great feedback! I'm glad I have help you, and thank you for the support!

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

    Your videos are the ones that take the longest to understand, but also the ones that are the most informative.

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

    Man this is one of my favorite js channels

  • @KentaroxKondo
    @KentaroxKondo 5 หลายเดือนก่อน

    I had been struggling with some problems in my project literally for days, but thanks to this video, I managed to find a solution. Thank you sooo much for setting me free! 😂

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

    Thanks Dave. Your tutorials are excellent. However, I want to suggest that you demarcate your output when you log it out on the console. So, it would be easy for someone to know what section of the code is being displayed at what section on the console. Outputs on the console can be confusing and diifficult to comphrehend without demarcation.

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

    I have watched a lot tuts on this thing .but the way u explaining every details is boosting my love for JS. Thanks a lot .

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

    Great video again. This is the best explanation of shallow vs deep copy in javascript. Thanks for sharing your knowledge!

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

    Wow I thought I was past this point but I learned aot and I like how you sum things up in the end.

  • @letshope5646
    @letshope5646 ปีที่แล้ว

    Dave, you have an exceptional knowledge in JavaScript. Much appreciated.Thanks

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

    You're a great teacher. You know so well how to convey the intended educational message about an advanced topic in accumulating small digestible chunks, helping one to have a solid understanding why things work this way, not just how.

  • @devworks8787
    @devworks8787 ปีที่แล้ว

    For many years I understood shallow and deep copy concept very wrongly. Damn!
    Thank you for this video!

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

    thank you, the video was simple but a little confusing with all the code in single file and the console outputs but very valuable :)

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

    Hi. A perfect explanation of fundamentals.
    I'm curious why Your channel is not that popular. You should collab with some channels like, Traversy Media, Dev Ed, and others so others can learn more. send a request for collab. I think They don't refuse.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว

      Thanks! I do have a video on Traversy Media and one on freeCodeCamp. No viral videos yet, but the channel is growing. 🚀 I just started last year, so I'll keep at it. 😃 Thanks for watching and subscribing. 🙏

  • @imakshith
    @imakshith หลายเดือนก่อน

    very informative David! thanks for sharing the content like this

  • @niranjanpandit6225
    @niranjanpandit6225 ปีที่แล้ว

    This is the best explaination of shallow vs deep copy in javascript. Keep it up Dave.
    You are a great teacher👍👍

  • @anshumanpati5434
    @anshumanpati5434 3 หลายเดือนก่อน

    Dave Thanks for making such good tutorials, none of the youtube channel have such precise content... :)

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

    Thanks Dave, very important information i was unware of. Thanks again

  • @christian-schubert
    @christian-schubert 3 ปีที่แล้ว

    Thank you very much for sharing this, probably the most comprehensive approach on this topic yet. The deepClone function is a really elegant proposition for nested data types, however, it doesn't seem to solve the problem copying some of the aforementioned complex data types pose (for instance nested Dates in objects).

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

      Thanks for watching and for the feedback. I'll have to explore that again in the future! Please share any further findings.

  • @adlerpazhouhan2170
    @adlerpazhouhan2170 ปีที่แล้ว

    Thank you Dave, it was a great explanation.

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

    Great stuff. I enjoy all your tutorials. Do you teach in a more formal setting? If so, where? I want to take some online advanced js course with projects.
    Again, thanks for the excellent material you produce🙏🏼

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

      Thank you, Mack! I'm currently a full-time dev, but also teach as an adjunct at FHSU. I was previously a full-time instructor before going back to industry.

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

    Such a Great Explanation. You Deserve more views. Thank you. Got many concepts clarified from this video

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

    Thanks.
    Another principle to practice independently, and exercises could have been awesome, but... very interesting indeed.

  • @BURBXN
    @BURBXN 3 ปีที่แล้ว

    Great video. Everything was thoroughly explained and covered completely. Thanks for sharing your knowledge!

  • @Robert-ed5dv
    @Robert-ed5dv 3 ปีที่แล้ว

    Well explained, just what I was looking for since I work with React and need to prevent the state from mutating when dealing with nested data structures.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว

      Great! Glad it was helpful. I'm planning some React tutorials for the summer. 😃

    • @Robert-ed5dv
      @Robert-ed5dv 3 ปีที่แล้ว

      Nice! Looking forward to that.

  • @tomislavzivkovic3978
    @tomislavzivkovic3978 ปีที่แล้ว

    Bravo!!! Finally explained understandable and clear.

  • @gordonmullen7272
    @gordonmullen7272 3 ปีที่แล้ว

    Thank you so much for explaining the details and can't wait for the follow-up with pue functions. It seems to me that we're going to see a lot about functional programming in the future or either a mixing, if you will, of oop , functional, and imperative to an extent.

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

    Great content

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

    man i give you a like and subscribed to your channel right away, in my opinion your lesson are valuable, because even advanced programmer still get this kinda stuff wrong , thank you so much

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

    Wonderful explanation Dave!!. Thanks

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

    I learnt lot of things in this one video itself

  • @vishnunair9933
    @vishnunair9933 3 ปีที่แล้ว

    Yet another great video, Dave! I hope you get more likes and subscriptions soon. You are one of the most underrated content creators in the tech space.

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

      Thank you! 🙏 The channel is growing, and I appreciate the support. 💯🚀

    • @vishnunair9933
      @vishnunair9933 3 ปีที่แล้ว

      @@DaveGrayTeachesCode Your series on JavaScript is amazing! I hope you make a video series on Sveltekit and Svelte someday. I know you are busy with the series on React :).

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

    A PRO classe of data structures!

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

    Thanks Dave!

  • @fieldmarshalgaig4856
    @fieldmarshalgaig4856 ปีที่แล้ว

    literally all my alevel coursework problems summed up right here

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

    Great explanation, thank u :)

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

    structuredClone() is used to make deep copy easier.

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

      Agreed. It wasn't available when I made this video. I could do another on it.

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

    This is a great video! Thanks Dave!
    A question here! When using React, I often use spread operator or concat to make copy from prev state of the object, but it seems like this is pointless to do so after watching your video. This is because with this React practice, it seems we are always doing shallow copy? What is the alternative to write instead if we wants to write a deep copy for setState for example? Correct me if I am mistaking something here!
    Thanks Dave!

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

      Great comment and question, Kim! If you have nested objects, a deep copy is ideal. Previously, we were creating our own deep copy function like in this tutorial or using another library with one like Lodash, but now, JS has recently added support for a native deep copy method called structuredClone. I need to make a tutorial about it: developer.mozilla.org/en-US/docs/Web/API/structuredClone ...Again, it is ok to use the spread operator most of the time, but nested object structures need this.

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

      @@DaveGrayTeachesCode wow! Didn't know structuredClone existed! This is very useful! Thank you for reply Dave! Really appreciate for sharing wisdom to us!

  • @GeorgiosAmsterdam
    @GeorgiosAmsterdam ปีที่แล้ว

    This is great stuff. Thanks

  • @גלמלאך
    @גלמלאך 2 ปีที่แล้ว

    The best video I ever seen!!

  • @vesnamohar4273
    @vesnamohar4273 6 หลายเดือนก่อน

    Thank you!

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

    Maybe you would like to explain obj.structuredClone() to simplify deep copy. :)

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

      Maybe so! I'm sure you already know structuredClone is a very newly supported feature. I created this tutorial in the middle of 2021 and according to caniuse.com/?search=structuredClone the earliest support for structuredClone in Chrome was Jan 31st, 2022. It seems like a great JS addition and would make a good follow up to this tutorial: developer.mozilla.org/en-US/docs/Web/API/structuredClone

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

    great job but maybe it does not work with Date properties))) you should to add "... || obj instanceof Date" to the condition of the first if statements.

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

      Nice note! 🙏 Looking at the new structuredClone now available in JS for a follow up to this. 🚀

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

    Please do some videos for advanced react concepts and complete react projects

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

      Please check the React Tutorials playlist on my channel, and also, I recently completed a MERN Stack Project series.

  • @DineshVutukuru
    @DineshVutukuru ปีที่แล้ว

    Wow, clean explanation

  • @doniaelfouly4142
    @doniaelfouly4142 4 หลายเดือนก่อน

    Thanks

  • @aeronwolfe7072
    @aeronwolfe7072 ปีที่แล้ว

    thank you so much

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

    Great video! correct me if I'm wrong but, I was under the impression that a for in loop did not always iterate sequentially. Could this cause a problem in the deepClone example function?

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

      No problems with an object and for in loops unless the keys are symbols. You may be remembering back to a discussion like this: stackoverflow.com/questions/38086774/does-the-for-in-loop-construct-preserve-order MDN does note that modifying during iteration can cause problems: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in If you have other references on this topic, I'm interested! 💯 In the meantime, you may also be interested in this video on maintaining synchronous iterations during async calls: th-cam.com/video/4lqJBBEpjRE/w-d-xo.html

  • @ahmad-murery
    @ahmad-murery 3 ปีที่แล้ว

    Very informative and must to know concepts in js,
    One should have a deepClone function in his utils.js toolbelt,
    Just a question regarding passing deepClone function as an argument to pureAddToScoreHistory function, can it be considered some sort of Dependency Injection?
    Thanks Dave

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

      I agree about adding this to your utility function module. 💯 And good question! My answer: yes, I agree! I wanted to be explicit with the arguments because a pure function should be. Great to see your comment! 🙏💯

    • @ahmad-murery
      @ahmad-murery 3 ปีที่แล้ว

      @@DaveGrayTeachesCode I hesitated a little bit before writing my comment but then I said I'll try it this time and see 😎
      Thanks, waiting for your next video

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

    Is it a Gary Holt Signature ESP guitar in the background?

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

      No, it has been months, but I think you are seeing my Peavey Wolfgang with custom EVH paint.

  • @quietcorner5989
    @quietcorner5989 ปีที่แล้ว

    Hey dave I have a question in the example of nested structural data type u said that vArray ad yArray have the same reference !! but when I log (vArray === yArray ) to the console I get false which means that they don't point to the same value in Memory !! please clarifiy this to me and also when I do vArray.push(5); instead of vArray[4].push(5); ==> yArray still the same and it didn't change its value!!

  • @FarhadAliev-z1f
    @FarhadAliev-z1f 2 หลายเดือนก่อน

    I wanted have a deep copy with pure function for this object const x = {
    name : 'Ali',
    x_funct: ()=>{
    console.log('Hello')
    },
    date : new Date()
    } but date is not visible when I am logging it {
    name: 'Ali',
    x_funct: [Function: x_funct],
    date: 2024-07-27T10:20:23.993Z
    }
    { name: 'Farhad', x_funct: [Function: x_funct], date: {} }

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

    Do you have any plans for bootstrap, scss?

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

      I may do Bootstrap in the future. Tailwind is trending. SASS/SCSS is always good. So many topics and not enough time! 💯

  • @sergiorey2049
    @sergiorey2049 3 ปีที่แล้ว

    Amazing

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

    How to compare both objects?

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

      That's what this tutorial is about. Notice the discussion of referential equality in the beginning of the tutorial.

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

    very good

  • @Fer-us4ny
    @Fer-us4ny ปีที่แล้ว

    github?

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

    How do i like this twice

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

    🚀

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

    • const isGreat = () => console.log('wonderful tutorial');
    • isGreat ();

  • @1000ylovers
    @1000ylovers 2 ปีที่แล้ว

    Hi Dave, thanks for the amazingly knowledgeable video. I have a question regarding the definition of shallow copy. From my research on geeksforgeeks and MDN docs, the definition of shallow copy differs from your definition and demonstration of shallow copy shown in this video around 8 minutes mark to 10 minutes mark. On MDN doc, it says “A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you may also cause the other object to change too - and so, you may end up unintentionally causing changes to the source or copy that you don't expect. That behavior contrasts with the behavior of a deep copy, in which the source and copy are completely independent.”
    developer.mozilla.org/en-US/docs/Glossary/Shallow_copy
    On the other hand, you said in the video around 9:50 mark that we’ve made a shallow copy using the spread operator, although the yArray and zArray are not sharing the same reference, which from my understanding differs from the definition of shallow copy (do share the same reference, thereby copy change causes the source change as well) written on MDN docs and GeeksforGeeks website.
    Can you clear my head plz. I’m losing it haha.
    Thank you! Your tutorials are so amazing and I’m so grateful for it :)

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

      That's a very thoughtful and well-researched question! Good work! It appears my use of the phrase "shallow copy" should be "shallow-clone" as referenced under MDN's spread syntax: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax - search for "shallow" on the page. The overall concept remains the same. Replace my use of the word copy for the word clone if it creates confusion. 💯🚀

    • @1000ylovers
      @1000ylovers 2 ปีที่แล้ว

      @@DaveGrayTeachesCode Thank you for your reply :)
      So is there a difference between shallow copy and shallow clone? Or is it just worded differently?

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

      At this point - after reviewing more of the MDN page - I'd say they are using the terms somewhat interchangeably. They state, "Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax..." and proceed to show spread syntax examples of making a shallow clone / copy. On the same page they state, "Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays, as the following example shows." - which is what I was showing in this video and how to make a deep copy if needed. Overall, don't let the wording confuse you. As long as you understand spread syntax will not copy / clone nested objects like a deep copy, you've got this.

    • @1000ylovers
      @1000ylovers 2 ปีที่แล้ว

      @@DaveGrayTeachesCode Thank you again for the very quick reply. I now understand haha. Yeah that Interchangeably using the copy and clone was throwing me off. You are amazing Dave!😄

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

    thank you!

  • @danilo86petrovic
    @danilo86petrovic ปีที่แล้ว

    This is so greatly explained... Thank you!