-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-IIFEs.js
More file actions
28 lines (20 loc) · 986 Bytes
/
11-IIFEs.js
File metadata and controls
28 lines (20 loc) · 986 Bytes
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
/**
* IIFEs stands for : Immediately Invoked Function Expressions
*/
// There’s another way to execute a function expression, which is typically referred to as an immediately invoked function expression
//Example:
(function salam(){
console.log( "Salam to all!" )
})();
// "Salam to all!"
//The outer ( .. ) that surrounds the (function salam(){ .. }) function expression is just a nuance of JS grammar needed to prevent it from being treated as a normal
//function declaration.
//The final () on the end of the expression—the })(); line—is what actually executes the function expression referenced immediately before it.
//That may seem strange, but it’s not as foreign as first glance. Consider the similarities between foo and IIFE here:
function salam() { console.log( "Salam to all!" ); }
// `foo` function reference expression,
// then `()` executes it
salam();
// `IIFE` function expression,
// then `()` executes it
(function salam(){ "Salam to all!" })();