Professional Javascript For Web Developers

- 06.43

photo src: medium.mybridge.co

An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript programming language idiom which produces a lexical scope using JavaScript's function scoping. Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function. This concept has been referred to as a self-executing anonymous function, but Ben Alman introduced the term IIFE as a more semantically accurate term for the idiom, shortly after its discussion arose on comp.lang.javascript.


JavaScript for Web Designers
photo src: www.lynda.com


Maps, Directions, and Place Reviews



Usage

Immediately-invoked function expressions may be written in a number of different ways. A common convention is to enclose the function expression (and optionally its invocation operator) with the grouping operator, i.e. in parentheses, to explicitly tell the parser to expect an expression. Otherwise, in most situations, when the parser encounters the function keyword, it treats it as a function declaration (statement), and not as a function expression.

There are other ways to enforce a function expression:

In contexts where an expression is expected, wrapping in parentheses is not necessary:

Passing variables into the scope is done as follows:

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:

...to avoid being parsed as c().


Professional Javascript For Web Developers Video



Examples

The key to understanding design patterns such as immediately-invoked function expressions is to realize that until recently JavaScript had only function scope (but not block scope) and passes values by reference inside a closure. (This is no longer entirely true, in the latest version of JavaScript block scoping is available and becomes evident when using the new let and const keywords.)

Evaluation context

A lack of block scope means that variables defined inside, for example, a for loop will have their definition "hoisted" to the top of the enclosing function. Evaluating a function that depends on variables modified by the outer function (including by iteration) can be difficult. We can see this without a loop if we update a value between defining and invoking the function.

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.

This is equivalent to the following code:

David Herman's Effective JavaScript contains an example illustrating the problems of evaluation context inside loops. While Herman's example is deliberately convoluted, it arises directly from the same lack of block scope.

Establishing private variables and accessors

IIFEs are also useful for establishing private methods for accessible functions while still exposing some properties for later use. The following example comes from Alman's post on IIFEs.

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.


Top 27 JavaScript Books for Web Developers 2017 - Colorlib
photo src: colorlib.com


Terminology

"Immediately-invoked function expression" as a term describes a design pattern that has also been referred to as a "self-executing anonymous function". However, immediately-invoked functions do not need to be anonymous, and ECMAScript 5's strict mode forbids arguments.callee, making the latter term less accurate.

In lambda calculus, this construct was referred to as "redex", for reducible expression, see Reduction strategy (code optimization).

Source of the article : Wikipedia



EmoticonEmoticon

 

Start typing and press Enter to search