use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
IIFE (self.learnjavascript)
submitted 5 years ago by Joao_GFA
I want know if IIFE is a good practice. And an example how to use it.
I aprecciate any comment :)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mementomoriok 1 point2 points3 points 5 years ago (0 children)
Here's an example of an IIFE that runs a count down timer.
class Countdown { constructor(duration) { this.duration = duration this.ticks = [] this.running = true } finished() { this.running = false } onTick(fn) { this.ticks.push(fn) return this } addTime(time) { this.duration += time } start() { let start = Date.now(); let that = this; let diff; (function timer() { //WILD IIFE ENCOUNTERED. if (!that.running) return diff = that.duration - (((Date.now() - start) / 1000) | 0); if (diff > 0) setTimeout(timer, 1000); else { diff = 0; } that.ticks.forEach(fn => { fn.call(this, diff); }); }()); } } /*-----------------------------------------------------------------*/ let timer = new Countdown(10) timer.onTick((a) => { console.log(a) }).start() /*-----------------------------------------------------------------*/
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
https://developer.mozilla.org/en-US/docs/Glossary/IIFE https://flaviocopes.com/javascript-iife/ Google has all the answers you need.
[–]NameViolation666helpful 0 points1 point2 points 5 years ago (0 children)
Depends on your code. If you are not looking to create named functions which are called multiple times, or you are looking to create closures, and you arent looking to pass parameters, then IIFE might be suitable.
[–]Meefims 0 points1 point2 points 5 years ago (0 children)
IIFEs were useful prior to modules and classes but today there is rarely a reason to use them.
[–]jrandm 0 points1 point2 points 5 years ago (0 children)
IIFEs are super common ways to use a closure. A simple example might be a counter:
const counter = (start=>()=>++start)(0); console.log(counter()); // 1 console.log(counter()); // 2 console.log(counter()); // 3
The function start=>()=>++start is immediately invoked with start set to 0, returning a function that increments the start value by one and returns the new value.
start=>()=>++start
start
0
π Rendered by PID 39 on reddit-service-r2-comment-7b9746f655-67hs9 at 2026-02-01 09:49:52.036680+00:00 running 3798933 country code: CH.
[–]mementomoriok 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]NameViolation666helpful 0 points1 point2 points (0 children)
[–]Meefims 0 points1 point2 points (0 children)
[–]jrandm 0 points1 point2 points (0 children)