Skip to content

Commit 741557d

Browse files
authored
Update 50-primitive-nonprimitive.md
1 parent 4cfdbca commit 741557d

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

notes/English/50-primitive-nonprimitive.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ person.sayHello(); // calling a method
5858
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.
5959

6060

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
6262
In JavaScript, primitives are stored by value, while non-primitives are stored by reference.
6363

6464
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
8484
```
8585

8686
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+
const obj1 = {a: 1, b: {c: 2}};
99+
const obj2 = {...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.
114+
115+
Example of deep copy:
116+
117+
```javascript
118+
const obj1 = {a: 1, b: {c: 2}};
119+
const obj2 = JSON.parse(JSON.stringify(obj1));
120+
121+
console.log(obj1); // {a: 1, b: {c: 2}}
122+
console.log(obj2); // {a: 1, b: {c: 2}}
123+
124+
obj2.a = 3;
125+
console.log(obj1); // {a: 1, b: {c: 2}}
126+
console.log(obj2); // {a: 3, b: {c: 2}}
127+
128+
obj2.b.c = 4;
129+
console.log(obj1); // {a: 1, b: {c: 2}}
130+
console.log(obj2); // {a: 3, b: {c: 4}}
131+
```

0 commit comments

Comments
 (0)