You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-26
Original file line number
Diff line number
Diff line change
@@ -908,38 +908,70 @@ obj1.myMethod(); // will print "Hi there" following with obj1.
908
908
909
909
```
910
910
911
-
## Question 26. What is JavaScript Self-Invoking anonymous function or Self-Executing anonymous function.
912
-
A `self-invoking` anonymous function also called `self-executing anonymous function` runs immediately or automatically when we define it and self-invoking anonymous function doesn't have any name at all. Self-Invoking anonymous function syntax:
911
+
## Question 26. What is IIFE (Immediately Invoked Function Expression) and how it can be useful?
912
+
### Answer
913
+
#### Definition
914
+
IIFE a function that runs as soon as it's defined. Usually it's anonymous (doesn't have a function name), but it also can be named. Here's an exmaple of IIFE:
913
915
914
916
```javascript
915
917
(function() {
916
-
console.log("Self-Invoking function code logic ");
918
+
console.log("Hi, I'm IIFE!");
917
919
})();
918
-
Output: Self-Invoking function code logic
919
-
```
920
-
921
-
We must have to know the fact that JavaScript functions run immediately when we put `()` after their names.
922
-
923
-
```javascript
924
-
function display() {
925
-
console.log("Display me");
926
-
}
927
-
display(); // This will run immediately
928
-
929
-
Output:"Display me"
930
-
/*
931
-
This will not run immediately as we haven't put () after function
932
-
name, function name is use full when we want to pass it as
933
-
callback to another function or method.
934
-
*/
935
-
display;
920
+
// outputs "Hi, I'm IIFE!"
921
+
```
922
+
#### Explanation
923
+
924
+
So, here's how it works. Remember the difference between function statements (`function a () {}`) and function expressions (`var a = function() {}`)? So, IIFE is a function expression. To make it an expression we surround our function declaration into the parens. We do it to explicitly tell the parser that it's an expression, not a statement (JS doesn't allow statements in parens).
925
+
926
+
After the function you can see the two `()` braces, this is how we run the function we just declared.
927
+
928
+
That's it. The rest is details.
929
+
- The function inside IIFE doesn't have to be anonymous. This one will work perfectly fine and will help to detect your function in a stacktrace during debugging:
930
+
```javascript
931
+
(functionmyIIFEFunc() {
932
+
console.log("Hi, I'm IIFE!");
933
+
})();
934
+
// outputs "Hi, I'm IIFE!"
935
+
```
936
+
- It can take some parameters:
937
+
```javascript
938
+
(functionmyIIFEFunc(param1) {
939
+
console.log("Hi, I'm IIFE, "+ param1);
940
+
})("Yuri");
941
+
// outputs "Hi, I'm IIFE, Yuri!"
942
+
```
943
+
Here there value `"Yuri"` is passed to the `param1` of the function.
944
+
- It can return a value:
945
+
```javascript
946
+
var result = (functionmyIIFEFunc(param1) {
947
+
console.log("Hi, I'm IIFE, "+ param1);
948
+
return1;
949
+
})("Yuri");
950
+
// outputs "Hi, I'm IIFE, Yuri!"
951
+
// result variable will contain 1
952
+
```
953
+
- You don't have to surround the function declaration into parens, although it's the most common way to define IIFE. Instead you can use any of the following forms:
954
+
-`~function(){console.log("hi I'm IIFE")}()`
955
+
-`!function(){console.log("hi I'm IIFE")}()`
956
+
-`+function(){console.log("hi I'm IIFE")}()`
957
+
-`-function(){console.log("hi I'm IIFE")}()`
958
+
-`(function(){console.log("hi I'm IIFE")}());`
959
+
-`var i = function(){console.log("hi I'm IIFE")}();`
Please don't use all these forms to impress colleagues, but be prepared that you can encounter them in someone's code.
936
966
937
-
functiontestCallBack(callback) {
938
-
callback (); // This will be call display method immediately if callback parameter is being set method display
939
-
}
940
-
testCallBack(display); // Here display function is being passed as callback
941
-
```
967
+
#### Applications and usefulness
942
968
969
+
Variables and functions that you declare inside an IIFE are not visible to the outside world, so you can:
970
+
- Use the IIFE for isolating parts of the code to hide details of implementation.
971
+
- Specify the input interface of your code by passing commonly used global objects (window, document, jQuery, etc.) IIFE’s parameters, and then reference these global objects within the IIFE via a local scope.
972
+
- Use it in closures, when you use closures in loops.
973
+
- IIFE is the basis of in the module pattern in ES5
974
+
code, it helps to prevent polluting the global scope and provide the module interface to the outside.
943
975
944
976
945
977
## Question 27. Describe Singleton Pattern In JavaScript?
0 commit comments