How to write an IIFE from scratch
In his post The anatomy of an immediately-invoked function expression, my friend Chris Ferdinandi shows you how he writes an IIFE from scratch. Here’s another way to approach it.
I start with a leading semicolon, two sets of parentheses, and a closing semicolon.
Then, I take my anonymous function:
And drop it into the first set of parentheses:
Boom. You’re good to go!
I also opt into strict mode to make my IIFE more bulletproof:
In addition to Chris’ resources, I recommend the following:
- What function window, document, undefined - window, document really means by Todd Motto
If you have questions, feedback, or any other suggestions, please do email me. I'd love to hear from you!