Skip to content

Commit cc16db4

Browse files
committed
complete section 10
1 parent fa44f5f commit cc16db4

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

10-A_Closer_Look_At_Functions/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,71 @@ const addTask = (rate = 0.23, value) => value + value * rate;
400400
```
401401
402402
But the difference is that the bind method allows us to create a completely different function we can work with.
403+
404+
# Immediately Invoked Function Expressions (IIFE)
405+
406+
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is called or invoked. This function will only run once
407+
408+
```js
409+
const runOnce = function () {
410+
console.log(`This will never run again`);
411+
};
412+
runOnce(); // Can run again
413+
```
414+
415+
The function above can be called again using the `runOnce` variable. What if we want to call this funtion only within itself, we use the `IIFE`
416+
417+
To use the `IIFE`, write a function without a variable name and wrap it inside parethesis(which converts it into an expression), and then we can call it using the parenthesis `()`.
418+
419+
```js
420+
(function () {
421+
console.log("Hello World");
422+
})();
423+
```
424+
425+
It also works with arrow fucntions
426+
427+
```js
428+
(() => console.log("Hello World 2"))();
429+
```
430+
431+
Since we didn't assign a variable on both functions, we can only run it once and there is no way to call it aside within itself.
432+
433+
### Importance of IIFE
434+
435+
- This **IIFE** is important when we want to handle Async, await operations.
436+
- Variables defined inside an `IIFE` scope will not be avaialble in the global scope. For example.
437+
438+
```js
439+
(function () {
440+
console.log("Hello World");
441+
const num = 23;
442+
})();
443+
444+
console.log(num);
445+
```
446+
447+
This will throw an error because `num` is not accessible in the global scope. Another word for this is called encapsulation.
448+
449+
Data encapsulation and Data privacy are extremely important concepts in programming. It allows us to protect variables from being overwritten by external scripts or libraries, and this is primarily the reason why the `IIFE` were introduced in programming.
450+
451+
Although it is not a method specific to the JavaScript language but a pattern developers came up with to enable them protect variables.
452+
453+
This is also similar to declaring variables using the `const` and `let` keywords within a code block.
454+
455+
```js
456+
{
457+
const isPrivate = 23;
458+
var notPrivate = 22;
459+
}
460+
```
461+
462+
The `var` keyword points to the global scope and so it will be accessible outside of the code block. While variables declared with `const` and `let` will create their own scope and will not be accessible outside of the code block.
463+
464+
In summary thanks to `ES6`, we do not need to use immediatelt invoked functions if all we want is to hide variables, we can simply declare them using `const` and `let`.
465+
466+
But if we want to run a function only once, `IIFE` is still the way to go, even now with modern JavaScript.
467+
468+
# Closures
469+
470+
WILL COME BACK TO THIS LESSON

10-A_Closer_Look_At_Functions/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<h1>A Closer Look at Functions</h1>
1212
<button class="buy">Buy new plane 🛩</button>
1313
<button class="poll">Answer poll ⁉️</button>
14-
<script src="challenge.js"></script>
14+
<script src="script.js"></script>
1515
</body>
1616
</html>

10-A_Closer_Look_At_Functions/script.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,33 @@ const addTaxRate = function (rate) {
217217
const addVat2 = addTaxRate(0.23);
218218
console.log(addVat2(100));
219219
console.log(addVat2(23));
220+
221+
// Immediately Invoked Function Expressions (IIFE)
222+
const runOnce = function () {
223+
console.log("This will never run again");
224+
};
225+
runOnce();
226+
227+
(function () {
228+
console.log("Hello World");
229+
})();
230+
231+
// Also works with arrow functions
232+
(() => console.log("Hello World 2"))();
233+
234+
// 🔸 Closures 🔸
235+
const secureBooking = function () {
236+
let passengerCount = 0;
237+
238+
return function () {
239+
passengerCount++;
240+
console.log(`${passengerCount} passengerCount`);
241+
};
242+
};
243+
244+
const booker = secureBooking();
245+
booker();
246+
booker();
247+
booker();
248+
249+
// WILL COME BACK TO THIS LESSON

0 commit comments

Comments
 (0)