-
Notifications
You must be signed in to change notification settings - Fork 0
/
interview1.js
92 lines (73 loc) · 2.62 KB
/
interview1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
In JavaScript, objects are reference types.
When you assign an object to a variable,
you are actually assigning a reference to
the object, not a copy of it. So in the given code,
both user2 and ans variables are pointing to the same object in memory.
When you change the age property of the ans object,
it will also change the age property of the user2
object because they are the same object.
To store the previous value of the user2 object,
you can create a copy of it before modifying it.
There are different ways to create a copy of
an object in JavaScript. One way is to use
the Object.assign() method to create a shallow copy of the object.
Here's an example code that demonstrates this:
*/
const user2 = {
name: "Rahul Kumar",
age: 24,
};
// create a copy of user2 object
const prevUser2 = Object.assign({}, user2);
// modify the age property of user2
user2.age = 65;
console.log(user2); // { name: "Rahul Kumar", age: 65 }
console.log(prevUser2); // { name: "Rahul Kumar", age: 24 }
// Deep Copy and Shallow Copy in javaScript
// [Value of objects can be changed by two mwthods : (refrence and By Value)]
let obj = {
name: "gaurav",
};
let user = obj; // {Here we assign obj Object to user Object
//where obj refrence is copied in user Object , and
//now both have same location and if we change value in any of these both Object's value will be changed}
// {Note}: this copy is done by (Refrence)
//Note: [Yedi jab hm variable ko copy krte hai to uski value copy hoti hai
// aur jab hm obj ko copy karte hai to uski location copy hoti hai]
//eg:
/* [let name='sunil';
let x=name;
x='anil'
console.log(x) // anil
console.log(name) // sunil]
*/
// Shallow Copy [It is for one level of object]
let obj1 = {
name: "Shyam",
};
const ans = { ...obj1 };
ans.name = "Sharma";
console.log(ans); //{ name: 'Sharma' }
console.log(obj1); //{ name: 'Shyam' }
// OR
const ans1 = Object.assign({}, obj1);
ans1.name = "Sudhir";
console.log(ans1); //{ name: 'Sudhir' }
console.log(obj1); //{ name: 'Shyam' }
// Deep Copy [It is for Nested object And Array]
// [Note]: if there will be function/methods inside nested object, It will not stringfigy/parse the function, other property will be manipulated
let userObj = {
name: "Sunil",
age: 98,
address: {
city: "Patna",
pin: 456756,
state: "Bihar",
},
};
const res = JSON.stringify(userObj);
const res1 = JSON.parse(res);
res1.address.pin = 800006;
console.log(res1); //{name: 'Sunil',age: 98,address: { city: 'Patna', pin: 800006, state: 'Bihar' }}
console.log(userObj); //{name: 'Sunil',age: 98,address: { city: 'Patna', pin: 456756, state: 'Bihar' }}