-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshallowDeepRestOperator.html
More file actions
50 lines (35 loc) · 1.34 KB
/
shallowDeepRestOperator.html
File metadata and controls
50 lines (35 loc) · 1.34 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Array Method</title>
</head>
<body></body>
<script>
let l = [10, 20, 30, 40, 50];
let m = l;
m.push(99);
console.log(l); // if m changes anything, l will be changed
console.log(m); // deep copy....l behavior also be copy in m
let l1 = [10, 20, 30, 40, 50];
let m1 = [...l]; // ...rest operator = Collects elements
m.push(99);
console.log(l1); // here not changed anything, bcz m = ... just only copy the values
console.log(m1); // shallow copy... only copy the values of l, not behavior
// l.push(99);
// console.log(l);
/* A shallow copy creates a new object/array that copies the top-level structure of the original,
but references the same underlying objects for nested properties/elements. */
const l3 = [10, 20, 30, [40, 50]];
const m3 = [...l3];
// Top-level property changed - doesn't affect original
m3[0] = 100;
// Nested property changed - affects original! reference value
m3[3][0] = 200;
console.log(l3); // 10 unchanged but 40 changed to 200
console.log(m3);
console.log(l3.join(", ")); //10, 20, 30, 200,50
console.log(m3.join(", ")); // 100, 20, 30, 200,50
</script>
</html>