-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIIFE.js
More file actions
79 lines (61 loc) · 1.32 KB
/
IIFE.js
File metadata and controls
79 lines (61 loc) · 1.32 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
/**
* Resources:
* 1. https://flaviocopes.com/javascript-iife/
* 2. https://stackabuse.com/javascripts-immediately-invoked-function-expressions/
* 3. https://medium.com/@vvkchandra/essential-javascript-mastering-immediately-invoked-function-expressions-67791338ddc6
*/
( // open IIFE
// Inner Anonymous Function
function () {
var tmp = "something";
console.log(tmp);
return tmp;
}
() // p
); // close IIFE
/**
** Passing Data
*/
( // open IIFE
// Inner Anonymous Function
function (data) {
var tmp = data; // not a global variable
console.log(tmp);
}
("something else")
); // close IIFE
// Arrow function
(() => {
/* */
})()
/**
* with unary operators
*/
+function () {
// Code that runs in your function
console.log("+");
}();
-function () {
// Code that runs in your function
console.log("-");
}();
!function () {
console.log("!");
// Code that runs in your function
}();
~function () {
// Code that runs in your function
console.log("~");
}();
void function () {
// Code that runs in your function
console.log("void");
}();
for (var i = 1; i <= 5; i++) {
(function (step) {
setTimeout(
() => console.log(`I reached step ${step}`),
100 * i
);
})(i);
}