Skip to content

Commit 3ec86ce

Browse files
committed
feat: 🎸 added shallow copy and deep copy eg
1 parent 3161c12 commit 3ec86ce

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://doesitmutate.xyz/
2+
/*
3+
A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made.
4+
5+
As a result, when you change either the source or the copy, you may also cause the other object to change too.
6+
7+
A shallow copy means that certain (sub-)values are still connected to the original variable.
8+
*/
9+
10+
/*
11+
12+
A deep copy means that all of the values of the new variable are copied and disconnected from the original variable.
13+
*/
14+
15+
const a = { a: 100 }
16+
const b = a // shallow copy i.e(mutates the source)
17+
const c = { ...a } // deep copy i.e(does not mutate the source)
18+
19+
console.log(a === b) // true
20+
console.log(a == b) // true
21+
console.log(a == c) // false
22+
console.log(a === c) // false
23+
24+
//* Nested objects
25+
26+
const a1 = {
27+
foods: {
28+
dinner: 'Pasta',
29+
},
30+
}
31+
let b1 = { ...a1 } // mutates a1
32+
// let b1 = structuredClone(a1) // does not mutates
33+
34+
b1.foods.dinner = 'Soup' // changes for both objects
35+
console.log(b1.foods.dinner) // Soup
36+
console.log(a1.foods.dinner) // Soup
37+
38+
//* How make deep copy of nested objects
39+
// const copy = JSON.parse(JSON.stringify(original))
40+
// const copy = structuredClone(original)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Primitive values:
3+
4+
These are the most basic values one can think of, which includes, undefined, null, boolean, string, and numbers. Primitive values are passed by value in javascript
5+
6+
Whereas all objects (including functions) are passed by reference in javascript.
7+
*/
8+
9+
/*
10+
Javascript pass by reference:
11+
In Pass by Reference, a function is called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function. In Javascript objects and arrays are passed by reference.
12+
*/
13+
14+
//javascript pass by reference
15+
function callByReference(varObj) {
16+
console.log('Inside Call by Reference Method')
17+
18+
varObj.a = 100
19+
20+
console.log(varObj)
21+
}
22+
23+
let myObj = {
24+
a: 1,
25+
}
26+
27+
console.log('Before Call by Reference Method')
28+
29+
console.log({ myObj }) // { myObj: { a: 1 } }
30+
31+
callByReference(myObj)
32+
33+
console.log('After Call by Reference Method')
34+
35+
console.log({ myObj }) // { myObj: { a: 100 } }
36+
37+
/*
38+
39+
In javascript pass by value, the function is called by directly passing the value of the variable as the argument. Therefore, even changing the argument inside the function doesn’t affect the variable passed from outside the function.
40+
41+
It is important to note that in javascript, all function arguments are always passed by value. That is, JavaScript copies the values of the passing variables into arguments inside of the function.
42+
*/
43+
44+
//javascript pass by value
45+
function square(x) {
46+
x = x * x
47+
48+
return x
49+
}
50+
51+
var y = 10
52+
53+
var result = square(y)
54+
55+
console.log(y) // 10 -- no change
56+
console.log(result) // 100
57+
58+
//
59+
const arr = [1, 2, 3, 4]
60+
61+
const updateArr = (arr, newValue) => arr.push(newValue) // Will mutate original array
62+
const updateArrWithoutMut = (arr, newValue) => [...arr].push(newValue) // Will not mutate original array
63+
64+
console.log({ arr })
65+
66+
// updateArr(arr, 10)
67+
updateArrWithoutMut(arr, 10)
68+
69+
console.log({ arr })

0 commit comments

Comments
 (0)