Deep Copying vs Shallow Copying

  Рет қаралды 23,477

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

6 жыл бұрын

When trying to create copies of the values inside variables in JavaScript it is very important that you understand the difference between passing by value and passing by reference.
This video talks about the differences between deep and shallow copies and how you can force a deep copy.
If you are working with NodeJS keep in mind that the JSON method is the only native one currently available.
Code GIST: gist.github.com/prof3ssorSt3v...

Пікірлер: 46
@Sam-rd5bo
@Sam-rd5bo 2 жыл бұрын
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.
@mokshdave6315
@mokshdave6315 4 жыл бұрын
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 !
@orimate
@orimate Жыл бұрын
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.
@learnweb4439
@learnweb4439 5 жыл бұрын
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.
@ahmedshahrour1643
@ahmedshahrour1643 5 жыл бұрын
Very simple and to the point! Thank you sir!
@rotrose7531
@rotrose7531 4 жыл бұрын
Thank you for this clear explanation about shallow and deep copy. Only you can make this difficult concept accessible to everyone.
@hou-yawang516
@hou-yawang516 4 жыл бұрын
Great tutorial as always. Thank you very much Steve! : )
@Edsiaren
@Edsiaren 3 жыл бұрын
Really good job explaining these. Thanks!
@sphiwemodise5713
@sphiwemodise5713 2 жыл бұрын
Well explained. Thank you 😊 I was struggling with this the whole day.
@priyankadey3337
@priyankadey3337 3 жыл бұрын
Thanks for the great examples! Awesome
@shubhamghosh123
@shubhamghosh123 4 жыл бұрын
I was super excited to see you using 'Calcutta' as an example of a place. I live in Calcutta (currently known as Kolkata) 😀
@chesterxp508
@chesterxp508 2 жыл бұрын
Another very cool tutorial!
@CameronChardukian
@CameronChardukian 5 жыл бұрын
This is currently a tough concept for me to grasp. I'll have to come back and watch this video again.
@yaolegoleynik
@yaolegoleynik 4 жыл бұрын
Thank you! Its really clear explanation :)
@kumargourav8892
@kumargourav8892 2 жыл бұрын
Great Stuff and great teaching ... Thanks
@LawZist
@LawZist 6 жыл бұрын
great as always
@prakarshshrivastava6422
@prakarshshrivastava6422 3 жыл бұрын
thank it was exactly what i was looking for
@ritsk4338
@ritsk4338 4 жыл бұрын
Awesome video... You have explained very clearly. thankyou
@priyankaamahour
@priyankaamahour 3 жыл бұрын
Thanks a million, Great explanation. you cleared all my doubts.
@linuxsport
@linuxsport 5 жыл бұрын
Thanks. It finally clicked.
@federicodubuch7199
@federicodubuch7199 5 жыл бұрын
Very good! thanks :D
@nosugerbei6977
@nosugerbei6977 4 жыл бұрын
You are the best!
@ricishrimple740
@ricishrimple740 3 жыл бұрын
i lost the whole day because of this problem, thanks a lot that you showed me how to solve it
@madeupstory
@madeupstory 3 жыл бұрын
A lot of great info in one video! thanks a lot!! It would have been nicer to see that custom recursive function as well :)
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
If you do a search you can find lots of existing examples of the recursive copying.
@Martin-xx2kw
@Martin-xx2kw 5 жыл бұрын
Bravo!
@vazhaabdu4529
@vazhaabdu4529 3 жыл бұрын
thank you sir
@praneetsingh9695
@praneetsingh9695 5 жыл бұрын
awesome...
@m0ZZaik
@m0ZZaik Жыл бұрын
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; }
@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.
@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 жыл бұрын
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.
@sirberbe
@sirberbe 5 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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".
@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.
@allezzthepunk
@allezzthepunk 3 жыл бұрын
Does the immutable.js package make it easier to deal with these kinds of issues and copy methods also?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
It is just the way the language works. So you need to understand it when you write your code.
@claudioandrade9292
@claudioandrade9292 2 жыл бұрын
whats the difference between shallow and deep copy and assign by reference or value?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 жыл бұрын
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.
@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?
@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. - kzbin.info/www/bejne/g2StgGenrbl_fpI
Everything You Feared about Base-64 Made Clear
19:27
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 2,9 М.
Shallow Copy and Deep Copy | Advanced Javascript Tutorial
26:36
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 50 МЛН
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 18 МЛН
Самое Романтичное Видео ❤️
00:16
Глеб Рандалайнен
Рет қаралды 3,5 МЛН
Understanding the Keyword THIS in JavaScript
13:59
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 8 М.
Visually Understanding JavaScript Prototypes
14:58
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 53 М.
Javascript Array and Object Cloning: Shallow or Deep?
8:39
Jack Herrington
Рет қаралды 11 М.
JavaScript - Reference vs Primitive Values/ Types
20:56
Academind
Рет қаралды 151 М.
Iterable vs Enumerable in JavaScript
9:15
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 20 М.
Copying Arrays (deep and shallow) - Beau teaches JavaScript
5:56
freeCodeCamp.org
Рет қаралды 45 М.
Composition vs Inheritance in JavaScript
12:23
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 11 М.
JavaScript Objects: Shallow and Deep Copy | All You Need to Know in 10 mins!
12:44
JavaScript Function Currying
11:41
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 15 М.
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 50 МЛН