Immediately invoked function expression
An immediately invoked function expression (or IIFE, pronounced "iffy" ) is a programming language idiom which produces a lexical scope using function scoping. It was popular in JavaScript as a method of supporting modular programming before the introduction of more standardized solutions such as CommonJS and ES modules. Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protecting against polluting the global environment and simultaneously allowing public access to methods while retaining privacy for variables defined within the function. In other words, it wraps functions and variables, keeping them out of the global scope and giving them a... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
An immediately invoked function expression (or IIFE, pronounced "iffy" ) is a programming language idiom which produces a lexical scope using function scoping. It was popular in JavaScript as a method of supporting modular programming before the introduction of more standardized solutions such as CommonJS and ES modules. Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protecting against polluting the global environment and simultaneously allowing public access to methods while retaining privacy for variables defined within the function. In other words, it wraps functions and variables, keeping them out of the global scope and giving them a local scope. (function () { /* ... */ })(); (function () { /* ... */ }()); (() => { /* ... */ })(); // With ES6 arrow functions (though parentheses only allowed on outside) In contexts where an expression is expected, wrapping in parentheses is not necessary: let f = function () { /* ... */ }(); true && function () { /* ... */ }(); 0, function () { /* ... */ }(); Passing variables into the scope is done as follows: (function(a, b) { /* ... */ })("hello", "world"); (function(a="hello", b="world") { /* ... */ })(); // also works An initial parenthesis is one case where the automatic semicolon insertion (ASI) in JavaScript can cause problems; the expression is instead interpreted as a call to the last term on the preceding line. In some styles that omit optional semicolons, the semicolon is placed in front of the parenthesis, and is known as a defensive semicolon. For example: a = b + c (function () { // code })(); ...to avoid being parsed as c() . ==Examples== The key to understanding design patterns such as IIFE is to realize that prior to ES6, JavaScript only featured function scope (thus lacking block scope), passing values by reference inside closures. This is no longer the case, as the ES6 version of JavaScript implements block scoping using the new let and const keywords. // Before ES6: Creating a scope using an IIFE var foo = 1; var bar = 2; (function(){ var foo = 3; // shadows the outer `foo` bar = 4; // overwrites the outer `bar` })(); console.log(foo, bar); // 1 4 // Since ES6: Creating a scope using curly brackets in combination with let and const const foo = 1; let bar = 2; { const foo = 3; // shadows the outer `foo` bar = 4; // overwrites the outer `bar` } console.log(foo, bar); // 1 4 let v, getValue; v = 1; getValue = function () { return v; }; v = 2; getValue(); // 2 While the result may seem obvious when updating v manually, it can produce unintended results when getValue() is defined inside a loop. Hereafter the function passes v as an argument and is invoked immediately, preserving the inner function's execution context. let v, getValue; v = 1; getValue = (function (x) { return function () { return x; }; })(v); v = 2; getValue(); // 1 This is equivalent to the following code: let v, getValue; v = 1; function f(x) { return function () { return x; }; }; getValue = f(v); v = 2; getValue(); // 1 // "counter" is a function that returns an object with properties, which in this case are functions. let counter = (function () { let i = 0; return { get: function () { return i; }, set: function (val) { i = val; }, increment: function () { return ++i; } }; })(); // These calls access the function properties returned by "counter". counter.get(); // 0 counter.set(3); counter.increment(); // 4 counter.increment(); // 5 If we attempt to access counter.i from the global environment, it will be undefined, as it is enclosed within the invoked function and is not a property of counter . Likewise, if we attempt to access i , it will result in an error, as we have not declared i in the global environment.
References
- Alman, Ben. Immediately Invoked Function Expressions . 15 November 2010. · 1787094000.0
- Strict mode . Mozilla JavaScript Ref . · 1787094000.0
- McGinnis, Tyler. JavaScript Modules: From IIFEs to CommonJS to ES6 Modules . ui.dev . · 1787094000.0
- Grouping operator . Mozilla Developer Network . 2 October 2023. · 1787094000.0
- Axel Rauschmayer. ExploringJS . · 1787094000.0
- JavaScript Semicolon Insertion: Everything you need to know . 28 May 2010. · 1787094000.0
- Marohnić, Mislav. Semicolons in JavaScript are optional . 7 May 2010. · 1787094000.0
- Orendorff, Jason. ES6 In Depth: let and const . Mozilla Hacks – the Web developer blog . · 1787094000.0
- Alman, Ben. simple-iife-example.js . Github . retrieved 5 February 2013 · 1787094000.0
- Baagoe, Johannes. Closing parenthesis in function's definition followed by its call . · 1787094000.0
Cite this
"Immediately invoked function expression". 1787150631. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1370187229 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy