There aren't that many options even in modern JavaScript. You have ES6 modules, store state in a global map, what else?
You don’t need a LOT of options. Just at least one that’s better and more extensible than this pattern.
The primary value of IIFE is that it is encapsulation that outputs to a value and data type. For example if an IIFE returns a string then it looks like a function, behaves like a function, but it is a primitive string type.
What language are you thinking of?
I now reach for this a lot to allow me to write expression oriented programs in JS
Usually I use it to encapsulate a complicated initialisation of a variable, where some mutation is needed/easier. I still want colocation for readability, but I dont want intermediate variables in the scope or to declare the variable as mutable
It's okay to name your functions. Flat is better than nested.
void function() {
…
}()
Escpecially when I can't be bothered to stop and extract an async function out when there's a sudden need to wait in a non-async context, handy when in the middle of adding features to browser extensions for apps: let $el = document.querySelector(…) // oops, not available yet
void async function() {
let $el = await waitForElement(…)
}()
Edit: documented on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
motorest•6mo ago
https://developer.mozilla.org/en-US/docs/Glossary/IIFE
lelandfe•6mo ago