You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shallow copy : In Shallow copy, It copy just first level of key and value. If value is any object or array then it will just copy the address of that value.
5
+
6
+
Note : You can not copy function in Shallow or Deep Copy. Its Possible Library function.
7
+
8
+
*/
9
+
10
+
11
+
varuser1={
12
+
name: 'Amit',
13
+
marks:{
14
+
maths:20,
15
+
}
16
+
};
17
+
18
+
console.log('----Shallow Copy using spread----');
19
+
varuser2={ ...user1};
20
+
console.log('\nuser2 :',user2);
21
+
console.log('\nuser1 :',user1);
22
+
23
+
user2.name='Harry';//this will apply only to user2
24
+
user2.marks.maths=10;// this will apply to user1 and user2
25
+
console.log('\nuser2 :',user2);
26
+
console.log('\nuser1 :',user1);
27
+
28
+
console.log('\n----Shallow Copy using Object.assign----');
29
+
varuser2=Object.assign({},user1);
30
+
console.log('\nuser2 :',user2);
31
+
console.log('\nuser1 :',user1);
32
+
33
+
user2.name='Harry';//this will apply only to user2
34
+
user2.marks.maths=10;// this will apply to user1 and user2
0 commit comments