Immediately invoked function expression

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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.

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.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1370187229 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001370187229 · captured 1787150631.0 · version history

Cite this page as: encyclopegia.org, “Immediately invoked function expression”, snapshot enwiki-001370187229. https://encyclopegia.org/a/immediately-invoked-function-expression

Sources cited in this revision

10 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.