Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Immediately Invoked Function Expressions

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is IIFEs (Immediately Invoked Function Expressions)?
 
Saloon Keeper
Posts: 12259
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't heard of this phrase before, but I assume it refers to defining a function and then immediately calling it. In older JavaScripts it was common to find code like this:

The reason for this was that it made it possible to execute code as soon as the script was called, but still allowing you to declare local variables without making them globally accessible.

I'm not sure if the practice is still used a lot in modern code with the advent of newer languages like ES6 and dependency loading frameworks like RequireJS.
 
Ishan Shah
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan van
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the construct is still used in modern JavaScript, though the needs to do so seem fewer within systems like React and Vue that when jQuery dominated.
 
Saloon Keeper
Posts: 22498
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
I'm not sure if the practice is still used a lot in modern code with the advent of newer languages like ES6 and dependency loading frameworks like RequireJS.



I've a NodeJS app that uses that stuff heavily (it consolidates data from multiple web APIs). But then, I've never felt compelled to continously chase the latest-and-greatest in JavaScript, so what do I know. I just found some good examples on Google and stole them.

Any resemblance between that kind of code and a Lambda expression is, I'm sure, purely co-incidental (snicker).
 
author
Posts: 284
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to use IIFE with modern JavaScript.

The point of IIFE was to introduce a new scope.




If you use let/const and arrow functions, you can simply use a block:



It is cleaner and easier to read.

You can also use IIFEs to implement modules, by returning an object that selectively exports from the scope. But in modern JavaScript you can just use modules.

Cheers,

Cay
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic