Introduction
Examples(function () { statements })();
The function becomes a function expression which is immediately executed. The variable within the expression can not be accessed from outside it.
(function () {
var aName = "Barry";
})();
// Variable name is not accessible from the outside scope
aName // throws "Uncaught ReferenceError: aName is not defined"
Assigning the IIFE to a variable stores the function's return value, not the function definition itself.
var result = (function () {
var name = "Barry";
return name;
})();
// Immediately creates the output:
result; // "Barry"
Link
Comments
Post a Comment