till this video I found the tutorials quite in-depth and easy to understand. Thanks for all your effort and time. do you have any html5 and reactjs tutorial ?
Hi, first of all, I am very thankful for your tutorials, but I am little bit confused about shallow copy explanation. As I know, shallow means not copy of object , it means sharing same local memory and has same reference, so if you change one of them object, other one will be affected. But if you copy object with Object.assign and then you add new method or property to object, the other object will not be affected. It is like Deep Copy, isn't it?
@@Techsithtube , I’m struggling with the concept of deep/shallow copy as well, let me make sure that I got this completely clearly: object.assign copies properties from one object literal to another; It does so in a manner of FULL COPY or CLONE so two objects are independent and you can change one object ant this change will not affect the other object; UNLESS you are copying object’s properties that are MORE THAN ONE LAYER DEEP in which case deeper proprties will be passed by reference, they will have a common source and changing one object will result in changing both. Is everything correct in this description?
@@egorsmirnov452 one-level copy means only primitive data types will be deep-copied. The composite (none primitive) data types (e.g arrays and objects) are shallow-copied (aka by pointer/reference only)
Hello, you said that prototype methods don't get copied but I tried the below code and type() method is in both objects, vitz and toyota. Why is that so? It should not be present in vitz object, right? let toyota = { drive() { return 'driving toyota'; } }; toyota.prototype = { type() { return 'Sedan'; } } let vitz = Object.assign({}, toyota); console.dir(vitz); console.dir(toyota);
what you see is an illusion you cant add the prototype to an object literal like this. its meant for constructors (functions) . the right syntax would be let toyota = { drive() { return 'driving toyota'; } }; Object.setPrototypeOf(toyota, { type: function() { return 'Sedan'; } }) let vitz = Object.assign({}, toyota); console.dir(vitz); console.dir(toyota);
Very well explained! No nonsense and just straight to the point. Thank you sir!
Thanks a lot for this wonderful series.
Great tutorials! Thank you so much!!!!
I spend more time in your channel and i suggest to all my colleague about your channel...Thanks man...
making javascript easy.
learning by examples approach instead of raw theory makes is far better.
excellent content.
techsith you are awesome! Thank you!
Wow nice one
Thanks for watching
Very informative series..thanks!
Thanks for watching!:)
Very useful.Thank you
excellent. Thank You
Hey so instead of Toyota you should give the break() method to Ford
u r the champ of JS
Thanks for nice comment ! )
Hey genius, I implement it ,Its work fine ,Thanks for sharing the videoes.
:) thanks for watching!
Very Useful
Thanks for watching!
till this video I found the tutorials quite in-depth and easy to understand. Thanks for all your effort and time.
do you have any html5 and reactjs tutorial ?
Yes I have a full series on react. If you look through my playlist
Found it !! Thanks :)
Didn't you forget a return statement in the last function?
Hi, first of all, I am very thankful for your tutorials, but I am little bit confused about shallow copy explanation. As I know, shallow means not copy of object , it means sharing same local memory and has same reference, so if you change one of them object, other one will be affected. But if you copy object with Object.assign and then you add new method or property to object, the other object will not be affected.
It is like Deep Copy, isn't it?
Shallow copy means copy one level down . so if you have an deeply nested object. after one level down you are just referencing the object not copying.
@@Techsithtube , I’m struggling with the concept of deep/shallow copy as well, let me make sure that I got this completely clearly: object.assign copies properties from one object literal to another; It does so in a manner of FULL COPY or CLONE so two objects are independent and you can change one object ant this change will not affect the other object; UNLESS you are copying object’s properties that are MORE THAN ONE LAYER DEEP in which case deeper proprties will be passed by reference, they will have a common source and changing one object will result in changing both. Is everything correct in this description?
@@egorsmirnov452 one-level copy means only primitive data types will be deep-copied. The composite (none primitive) data types (e.g arrays and objects) are shallow-copied (aka by pointer/reference only)
Is it a good practice to use Object.assign method in constructor?
As long as your bowser supports Object.assign you should use it .
Hello, you said that prototype methods don't get copied but I tried the below code and type() method is in both objects, vitz and toyota.
Why is that so? It should not be present in vitz object, right?
let toyota = {
drive() {
return 'driving toyota';
}
};
toyota.prototype = {
type() {
return 'Sedan';
}
}
let vitz = Object.assign({}, toyota);
console.dir(vitz);
console.dir(toyota);
what you see is an illusion you cant add the prototype to an object literal like this. its meant for constructors (functions) . the right syntax would be
let toyota = {
drive() {
return 'driving toyota';
}
};
Object.setPrototypeOf(toyota, {
type: function() {
return 'Sedan';
}
})
let vitz = Object.assign({}, toyota);
console.dir(vitz);
console.dir(toyota);
Oh yes, Thank you so much for pointing out the mistake and clearing the concept.
i dont understand the last part
Can u pls exaplain shallow and deep copy in little detail?
Shallow means a copy of the object and its methods. a deep copy will include not only object data but its prototype properties as well. got it?
yes got ..thanku so much
I thought in objects it needed to be drive: function(){},
in the ES6/ES2015 you can use drive(){}
techsith thanks
Is this advanced JAVASCRIPT
it's not advance javaScript. but most of the people don't use it.
4:00 , actually Object.assign() doesnt make the shallow copy, instead makes the deep copy with below exception, please check this MDN example;
(If the source value is a reference to an object, it only copies the reference value.)
let obj1 = { a: 0 , b: { c: 0}};
let obj2 = Object.assign({}, obj1);
console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
obj1.a = 1;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
obj2.a = 2;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 0}}
obj2.b.c = 3;
console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 3}}
console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 3}}
That is consider as a shallow copy. THat is why your last example has the same C value. Deep copy will create copy of everything.
@@Techsithtube Shallow copy of every prop individually not the obj itself, got you thanks
check this out..
object.assign( [ ], obj ). Magic :). Not a shallow copy right? Or am i just writing rubbish?
Its is a shallow copy of Obj.