Deep Copying vs Shallow Copying

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ •

  • @Sam-rd5bo
    @Sam-rd5bo 2 роки тому +3

    Almost no one is explaining this topic this way.
    About everyone else is making it more complex then it actually is.
    Thank you so much.

  • @orimate
    @orimate 2 роки тому

    So many thanks for this explaining. After 2 days of trying to get work my code I founded your video. Now I know what is my problem.

  • @mokshdave6315
    @mokshdave6315 4 роки тому +9

    Thank you soo much. This video actually helped alot .... You cleared so many doubts that other videos on shallow and deep copying were'nt able to do. Thanks again. Cheers !

  • @rotrose7531
    @rotrose7531 4 роки тому +1

    Thank you for this clear explanation about shallow and deep copy. Only you can make this difficult concept accessible to everyone.

  • @shubhamghosh123
    @shubhamghosh123 4 роки тому +2

    I was super excited to see you using 'Calcutta' as an example of a place. I live in Calcutta (currently known as Kolkata) 😀

  • @learnweb4439
    @learnweb4439 6 років тому

    Best explanation of deep / shallow copy. Many thanks Steve.

  • @jaltoorey4445
    @jaltoorey4445 Рік тому

    Awesome. I feel like I appreciate this as much as you would have hoped one does. ty.

  • @sphiwemodise5713
    @sphiwemodise5713 3 роки тому +1

    Well explained. Thank you 😊
    I was struggling with this the whole day.

  • @ahmedshahrour1643
    @ahmedshahrour1643 5 років тому +2

    Very simple and to the point! Thank you sir!

  • @CameronChardukian
    @CameronChardukian 5 років тому +2

    This is currently a tough concept for me to grasp. I'll have to come back and watch this video again.

  • @hou-yawang516
    @hou-yawang516 4 роки тому +1

    Great tutorial as always. Thank you very much Steve! : )

  • @priyankaamahour
    @priyankaamahour 4 роки тому +1

    Thanks a million, Great explanation. you cleared all my doubts.

  • @madeupstory
    @madeupstory 3 роки тому +1

    A lot of great info in one video! thanks a lot!!
    It would have been nicer to see that custom recursive function as well :)

  • @ricishrimple740
    @ricishrimple740 3 роки тому

    i lost the whole day because of this problem, thanks a lot that you showed me how to solve it

  • @Edsiaren
    @Edsiaren 4 роки тому

    Really good job explaining these. Thanks!

  • @kumargourav8892
    @kumargourav8892 3 роки тому

    Great Stuff and great teaching ... Thanks

  • @m0ZZaik
    @m0ZZaik 2 роки тому +1

    I was working on a custom function that deepcopies objects (functions, and arrays aswell) and every property/element down the tree recursively;
    So far, it seems to work in all scenarios, but maybe I am missing something:
    function deepCopy(object) {
    let objCopy;
    if (Array.isArray(object)) {
    objCopy = [...object];
    for (let i = 0; i < object.length; i++) {
    if (typeof object[i] === 'object') {
    objCopy[i] = deepCopy(object[i]);
    }
    }
    } else if (typeof object === 'function'){
    objCopy = object.bind();
    } else if (typeof object === 'object'){
    objCopy = {...object};
    const keys = Object.keys(object);
    for (let i = 0; i < keys.length; i++) {
    if (typeof object[keys[i]] === 'object') {
    objCopy[keys[i]] = deepCopy(object[keys[i]]);
    }
    }
    } else {
    objCopy = object;
    }
    return objCopy;
    }

  • @ritsk4338
    @ritsk4338 5 років тому

    Awesome video... You have explained very clearly. thankyou

  • @priyankadey3337
    @priyankadey3337 3 роки тому

    Thanks for the great examples! Awesome

  • @allezzthepunk
    @allezzthepunk 4 роки тому

    Does the immutable.js package make it easier to deal with these kinds of issues and copy methods also?

  • @yaolegoleynik
    @yaolegoleynik 5 років тому +1

    Thank you! Its really clear explanation :)

  • @chesterxp508
    @chesterxp508 3 роки тому

    Another very cool tutorial!

  • @generationwolves
    @generationwolves 3 роки тому

    Some additional info:
    1. Be cautious while using the JSON method for deep copy. Objects like functions, dates, etc will be dropped, as JSON does not support these data types.
    2. If you want a third-party solution for deep copy, look no further than lodash's cloneDeep() method. This method preserves all data types, and is the easiest method IMO.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 роки тому

      lodash's cloneDeep method still only works for things that are structured clonable.
      It is designed to work the same way as structured clonable works with JSON method or postMessage and the other built-in copy methods.

  • @claudioandrade9292
    @claudioandrade9292 3 роки тому

    whats the difference between shallow and deep copy and assign by reference or value?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 роки тому

      They are essentially the same idea but in javascript it all comes down to whether the variable holds a primitive or an object. All objects are referenced. All primitive pass their value. Doesn't matter how or where things are passed.

  • @sirberbe
    @sirberbe 5 років тому +1

    When we deep copy objects using JSON.stringify and JSON.parse we lose our methods if we had, So what is right way of copying objects without losing methods or stuffs like that?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 років тому +4

      Any kind of copying with JSON or web workers or notifications only works for primitive datatypes. If you want to copy complex objects that include functions then you need to write your own custom recursive function to copy or consider making new objects and use the old one as the prototype.

    • @ahmedshahrour1643
      @ahmedshahrour1643 5 років тому +1

      Had that question popping up as I was going through the video. Steve's reply is on point, recursive usually has a reputation for being inefficient and there's no way to go around the issue. There's a library out there that handles the mess called "Lodash" that has a method called "copyDeep".

  • @FF-ie6sd
    @FF-ie6sd Рік тому

    Not sure if I missed anything, but I dont think lodash deepclone is mentioned here. Very easy way to make deep clone.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Рік тому

      I did not discuss any of the libraries that have methods to do the deep cloning. They do the recursive function calls to copy objects.
      There is now a new structuredClone method built into JS. - ua-cam.com/video/M3JO4qxTLH0/v-deo.html

  • @TusharBorawake
    @TusharBorawake 5 років тому

    How spread operator, array.concat(), array.from & array.slice become shallow copy ? it won't change original array/object.can you please elaborate.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 років тому +1

      Shallow copying is just what JS does. If the contents of the array are primitive values then you get a new copy of the value. If the elements in the array are Objects then you just get references to those objects in the new array.

  • @S4sent
    @S4sent 5 років тому

    We can use jquery extend method for deep copy

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 років тому

      You can write your own functions to do deep copy, or use Web Notifications, Web Workers, localStorage, or the JSON object to do deep copying. No need to use jQuery. Ever.

  • @martinbozinovski
    @martinbozinovski 5 років тому

    I tried [ ] instead of { } for the Object.assign( [ ], (source). I don't think that is a shallow copy. Or am i just talking nonsense?

  • @prakarshshrivastava6422
    @prakarshshrivastava6422 4 роки тому

    thank it was exactly what i was looking for

  • @LawZist
    @LawZist 6 років тому +2

    great as always

  • @linuxsport
    @linuxsport 5 років тому

    Thanks. It finally clicked.

  • @nosugerbei6977
    @nosugerbei6977 4 роки тому

    You are the best!

  • @federicodubuch7199
    @federicodubuch7199 6 років тому

    Very good! thanks :D

  • @vazhaabdu4529
    @vazhaabdu4529 4 роки тому

    thank you sir

  • @praneetsingh9695
    @praneetsingh9695 6 років тому

    awesome...

  • @Martin-xx2kw
    @Martin-xx2kw 6 років тому

    Bravo!