⏱ 0:01est. 3 min
Chapter 45- Framework Aside (IIFEs and safe Code)
Notes
Example 1
//IIFE
(function (name){
var greeting = "Inside IIFE ";
console.log(greeting + name);
}(‘John'))
//OUTPUT : Inside IIFE John
Explanation -
Creating function on the fly and executing it, When the code is executed It creates global execution context and nothing is in it as we don't have any global variables or function statements to be hoisted. Then it hits immediately invoked function expression. Then it creates that function object in memory.
Then a new execution context is created for that anonymous function then code is run line by line in that function. All variables will be declared in that new execution context
What is a closure?
A closure is an inner function that has access to the outer (enclosing) function's variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables.
The inner function has access not only to the outer function's variables, but also to the outer function's parameters. Note that the inner function cannot call the outer function's arguments object, however, even though it can call the outer function's parameters directly.
http://javascriptissexy.com/understand-javascript-closures-with-ease/
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36