js2

Immediately-Invoked Function Expression Pattern (IIFE)

What is an “Immediately-Invoked Function Expression” (IIFE)?

is a JavaScript design pattern 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.

A “Self-executing anonymous function” or “Immediately-Invoked Function Expression”?

IIFE, pronounced “iffy”, is a pattern has been referred to as a “self-executing anonymous function”. Both terms are pointing at the same idea, but IIFE (Ben Alman) make more sense, in my opinion.

Why “Immediately-Invoked Function Expression” (IIFE) is more accurate term?

– It’s not self-executing, because it doesn’t invoke itself. It is Function Expression that gets invoked immediately, however.

– Doesn’t needs to be “anonymous” it could be since either “anonymous” or “named”.

Let’s code the words

or with a name

Defining public accessors

Parentheses order

Both expressions below are correct

Defensive semicolon

In JavaScript can cause problems and being interpreted as a call to the last term on the preceding line. Normally we use this if we are extending or creating a plugin.

Further reading

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *