-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path062-iife.js
More file actions
129 lines (103 loc) · 2.62 KB
/
062-iife.js
File metadata and controls
129 lines (103 loc) · 2.62 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// WHAT IS IIFE?
function greet() {
console.log("Hello!");
}
greet(); // We call it here
(function(){
console.log("Hello")
})();
(function() {
console.log("Hello!");
}());
// WHY USE IIFE?
// Reason #1: Creating Private Scope
(function() {
const privateVariable = "I'm private!";
console.log(privateVariable); // Works fine here
})();
console.log(privateVariable); // Error! Not accessible outside
// Reason #2: Avoiding Global Namespace Pollution
// const counter = 0; // From developer A
// const counter = 10; // From developer B - Oops, conflict!
(function() {
const counter = 0;
// Developer A's code
})();
(function() {
const counter = 10;
// Developer B's code
})();
// IIFE WITH PARAMETERS
const globalValue = "I'm global";
(function(value) {
console.log(value); // "I'm global"
const localValue = "I'm local";
console.log(localValue);
})(globalValue);
(function(config) {
const apiUrl = config.url;
const apiKey = config.key;
console.log(`Connecting to ${apiUrl}`);
// Your API logic here
})({ url: "https://api.example.com", key: "abc123" });
// RETURNING VALUES FROM IIFE
const calculator = (function() {
// Private variable
const secretMultiplier = 2;
// Private function
function privateAdd(a, b) {
return a + b;
}
// Public API
return {
add: function(a, b) {
return privateAdd(a, b);
},
multiply: function(a, b) {
return a * b * secretMultiplier;
}
};
})();
console.log(calculator.add(5, 3)); // 8
console.log(calculator.multiply(5, 3)); // 30
console.log(calculator.secretMultiplier); // undefined - it's private!
// ARROW FUNCTION IIFE
(() => {
console.log("Arrow function IIFE!");
})();
// With parameters
((name) => {
console.log(`Hello, ${name}!`);
})("JavaScript");
// REAL-WORLD USE CASES
// Use Case 1: Initialization Code
(function() {
// Run setup code once
const appConfig = {
theme: "dark",
language: "en"
};
document.body.setAttribute("data-theme", appConfig.theme);
console.log("App initialized!");
})();
// Use Case 2: Loop Variable Encapsulation
// Problem with var
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i); // Prints 3, 3, 3
}, 1000);
}
// Solution with IIFE
for (var i = 0; i < 3; i++) {
(function(index) {
setTimeout(function() {
console.log(index); // Prints 0, 1, 2
}, 1000);
})(i);
}
// MODERN ALTERNATIVES
{
const privateValue = "I'm block-scoped!";
console.log(privateValue);
}
// privateValue is not accessible here