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
Copy file name to clipboardExpand all lines: notes/English/50-primitive-nonprimitive.md
+46-1Lines changed: 46 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ person.sayHello(); // calling a method
58
58
In summary, primitives are simple data types that are immutable, while objects are complex data types that are mutable and can hold multiple values and methods.
59
59
60
60
61
-
### Understanding of how primitive and non-primitives are stored in memory in JavaScript. Explain in detail with example.
61
+
### Understanding of how primitive and non-primitives are stored in memory in JavaScript
62
62
In JavaScript, primitives are stored by value, while non-primitives are stored by reference.
63
63
64
64
Primitives include numbers, strings, booleans, null, and undefined. When a primitive is assigned to a variable, a copy of its value is created and stored in memory at a specific location. For example:
@@ -84,3 +84,48 @@ console.log(arr2); // [1, 2, 3, 4] (since arr2 contains a reference to the same
84
84
```
85
85
86
86
In this example, `arr1` is assigned an array with three elements. When `arr2` is assigned to `arr1`, a reference to the memory location where `arr1` is stored is created and stored in memory for `arr2`. Therefore, when an element is added to `arr1`, it affects the value of `arr2` as well since they both point to the same memory location.
87
+
88
+
89
+
### How to Copy object? Shallow copy and deep copy ?
90
+
91
+
In JavaScript, you can copy an object in two ways: shallow copy and deep copy.
92
+
93
+
A shallow copy creates a new object with the same properties as the original object, but the properties are simply references to the same values as the original object. This means that if you modify a property of the copied object or the original object, both objects will be affected. Shallow copying can be done using the spread operator (`...`) or `Object.assign()` method.
94
+
95
+
Example of shallow copy:
96
+
97
+
```javascript
98
+
constobj1= {a:1, b: {c:2}};
99
+
constobj2= {...obj1};
100
+
101
+
console.log(obj1); // {a: 1, b: {c: 2}}
102
+
console.log(obj2); // {a: 1, b: {c: 2}}
103
+
104
+
obj2.a=3;
105
+
console.log(obj1); // {a: 1, b: {c: 2}}
106
+
console.log(obj2); // {a: 3, b: {c: 2}}
107
+
108
+
obj2.b.c=4;
109
+
console.log(obj1); // {a: 1, b: {c: 4}}
110
+
console.log(obj2); // {a: 3, b: {c: 4}}
111
+
```
112
+
113
+
A deep copy creates a new object with the same properties as the original object, but the properties are not references to the same values as the original object. This means that modifying a property of the copied object does not affect the original object, and vice versa. Deep copying can be done using several methods, including recursion or libraries such as Lodash.
0 commit comments