Skip to content

Commit 8bb7488

Browse files
committed
complete passing arguments by value and reference
1 parent 1986292 commit 8bb7488

File tree

4 files changed

+70
-59
lines changed

4 files changed

+70
-59
lines changed

10-A_Closer_Look_At_Functions/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function createBooking(flightNum, numPassengers, price) {
1414
console.log(booking);
1515
bookings.push(booking);
1616
}
17-
1817
createBooking("LH123");
1918
```
2019

@@ -93,3 +92,45 @@ In a situation we want to skip a parameter, we can simply assign it `undefined`,
9392
```js
9493
createBooking("LH123", undefined, 1000); // numPassengers = 1, price = 1000
9594
```
95+
96+
## How Passing Arguments Works (Values vs Reference)
97+
98+
Passing a primitive type to a function is the same as creating a copy outside of the function, so the value is simply coppied. On the other hand when we pass an object to a function, it is just like copying the object, so whatever we change in the copy will also affect the original.
99+
100+
For Example:
101+
102+
```js
103+
const flight = "LH234";
104+
const eke = {
105+
name: "Victor Eke",
106+
passport: 24739479284,
107+
};
108+
109+
const checkIn = function (flightNum, passenger) {
110+
flightNum = "LH999";
111+
passenger.name = `Mr. ${passenger.name}`;
112+
113+
if (passenger.passport === 24739479284) {
114+
alert("Check in");
115+
} else {
116+
alert("Wrong passport");
117+
}
118+
};
119+
120+
checkIn(flight, eke);
121+
console.log(flight); // "LH234"
122+
console.log(eke); // Mr Victor Eke
123+
```
124+
125+
Another example
126+
127+
```js
128+
const newPassport = function (person) {
129+
person.passport = Math.trunc(Math.random() * 10000000000);
130+
};
131+
132+
newPassport(eke);
133+
checkIn(flight, eke);
134+
```
135+
136+
In programming there are two terms used all the time when dealing with functions: `passing by value` and `passing by reference`. JavaScript does not have `passing by reference`, only `passing by value`.

10-A_Closer_Look_At_Functions/css/main.css

Lines changed: 0 additions & 57 deletions
This file was deleted.

10-A_Closer_Look_At_Functions/css/main.css.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

10-A_Closer_Look_At_Functions/script.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,31 @@ createBooking("LH123", 2, 800);
2525
createBooking("LH123", 2); // "LH123" 2 * 199 = 398
2626
createBooking("LH123", 5); // "LH123" 5 * 199 = 995
2727
createBooking("LH123", undefined, 1000); // 1(default value), 1000
28+
29+
const flight = "LH234";
30+
const eke = {
31+
name: "Victor Eke",
32+
passport: 24739479284,
33+
};
34+
35+
const checkIn = function (flightNum, passenger) {
36+
flightNum = "LH999";
37+
passenger.name = `Mr. ${passenger.name}`;
38+
39+
// if (passenger.passport === 24739479284) {
40+
// alert("Check in");
41+
// } else {
42+
// alert("Wrong passport");
43+
// }
44+
};
45+
46+
checkIn(flight, eke);
47+
console.log(flight); // "LH234"
48+
console.log(eke); // Mr Victor Eke
49+
50+
const newPassport = function (person) {
51+
person.passport = Math.trunc(Math.random() * 10000000000);
52+
};
53+
54+
newPassport(eke);
55+
checkIn(flight, eke);

0 commit comments

Comments
 (0)