3

It is clear that in EcmaScript, functions when invoked create a new execution context. All variables and functions defined within a function may only be accessed inside that function scope. But when we use closures variables and functions may be accessed outside that context. IIFE is a function expression that gets invoked immediately. It is simple.

But why are IIFE different from Self-Executing Anonymus Functions, it is not completely clear to me!?

jjpcondor
  • 1,326
  • 1
  • 18
  • 27
  • Relevant article: http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – Felix Kling Jun 05 '13 at 12:19
  • Related to self executing anonymous functions: https://stackoverflow.com/questions/56108429/immediately-invoke-a-function-declaration-without-using-iife-pattern – Aaditya Sharma May 13 '19 at 09:16

1 Answers1

8

They are the same, it was renamed to IIFE because an IIFE is not necessarily anonymous, and they do not execute themselves.

consider the following:

(function bleh() {
    alert('I am not anonymous, i have a name!');
})(); //<-- invoked like any other function
epoch
  • 15,647
  • 3
  • 39
  • 67
  • *renamed* might not be the right term, there are many names for the same concept – "Immediate functions", "self-executing functions", etc – David Hellsing Sep 20 '12 at 08:36
  • I have found an excellent explanation given by Mark Dalgleish here: http://www.youtube.com/watch?feature=player_detailpage&v=KRm-h6vcpxs#t=699s – 1) IIFE do not execute them selves, they are invoked like any other function – 2) IIFE do not have to be anonimous, it is only important that they are function expressions. – jjpcondor Sep 20 '12 at 10:19
  • Mea culpa -- my distraction – jjpcondor Dec 24 '15 at 17:41