diff --git a/README.md b/README.md index 60debd6..1e06354 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ __________________ 18. How could you cache execution of any function? 19. If you need to implement the following chaining with call back, how will you implement it? 20. How could you implement moveLeft animation? -21. How would you implement currying for any functions? +21. How would you implement currying for any functions? +22. What is IIFEs (Immediately Invoked Function Expressions)? #### [JS: Answer for Basics and Tricky Questions](http://www.thatjsdude.com/interview/js2.html) diff --git a/js2.html b/js2.html index 4c2a2b1..dd38700 100644 --- a/js2.html +++ b/js2.html @@ -129,6 +129,7 @@
ref: Favoring Curry, curry: cooking up tastier functions
- + +Question: What is IIFEs (Immediately Invoked Function Expressions)?
+Answer:It’s an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it’s created:
+
+(function IIFE(){
+ console.log( "Hello!" );
+})();
+// "Hello!"
+
+ This pattern is often used when trying to avoid polluting the global namespace, because all the variables used inside the IIFE (like in any other normal function) are not visible outside its scope.
+