Skip to content

Commit dd1863d

Browse files
committed
q26 completely rewritten
1 parent b9b2054 commit dd1863d

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

README.md

+58-26
Original file line numberDiff line numberDiff line change
@@ -908,38 +908,70 @@ obj1.myMethod(); // will print "Hi there" following with obj1.
908908

909909
```
910910

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:
913915

914916
```javascript
915917
(function() {
916-
console.log("Self-Invoking function code logic ");
918+
console.log("Hi, I'm IIFE!");
917919
})();
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+
(function myIIFEFunc() {
932+
console.log("Hi, I'm IIFE!");
933+
})();
934+
// outputs "Hi, I'm IIFE!"
935+
```
936+
- It can take some parameters:
937+
```javascript
938+
(function myIIFEFunc(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 = (function myIIFEFunc(param1) {
947+
console.log("Hi, I'm IIFE, " + param1);
948+
return 1;
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")}();`
960+
- `true && function(){ console.log("hi I'm IIFE") }();`
961+
- `0, function(){ console.log("hi I'm IIFE") }();`
962+
- `new function(){ console.log("hi I'm IIFE") }`
963+
- `new function(){ console.log("hi I'm IIFE") }()`
964+
965+
Please don't use all these forms to impress colleagues, but be prepared that you can encounter them in someone's code.
936966

937-
function testCallBack(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
942968

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.
943975

944976

945977
## Question 27. Describe Singleton Pattern In JavaScript?

0 commit comments

Comments
 (0)