IIFE Parentheses
See original GitHub issueCould this:
(function() {
console.log('Welcome to the Internet. Please follow me.');
})();
Be this? :
// Crockford's preference - parens on the inside
(function() {
console.log('Welcome to the Internet. Please follow me.');
}());
Issue Analytics
- State:
- Created 11 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Immediately-Invoked Function Expressions and Parentheses
Let's explore the two IIFE methods using parentheses a bit more. Both work the same. It starts to get interesting when one of...
Read more >Why are parentheses required around JavaScript IIFE?
The version of IIFE that is wrapped in parenthesis works, because this marks the declaration of the internal function declaration as ...
Read more >Why do you need parentheses for an IIFE if function ... - Reddit
So why is it necessary to wrap it in parentheses to make it an IIFE? What's the parsing rule that causes this?
Read more >The many ways to write an Immediately Invoked Function ...
An Immediately Invoked Function Expression, or IIFE, is a function that immediately and automatically invokes itself and runs the code inside.
Read more >IIFE | Dev Cheatsheets - Michael Currin
An IIFE is function that executes immediately after it is defined (usually ... IIFE. Surround the function in brackets before calling it will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you don’t care about the return value of the IIFE, it could also be any of the following:
Let’s explore this a bit more.
Both work the same. It starts to get interesting when one of the modules is missing a trailing semicolon:
With a missing semicolon, each set of parens is trying to immediately-invoke the preceding expression. That would be the return value of the preceding IIFE.
So the difference is when the TypeError happens. Let’s check out what the arguments are up to. Note that
console.log()
returnsundefined
:Now let’s do that same example with the crockford way:
But wait, there’s no TypeError here…
There’s no TypeError because of the returned function. The returned function that logs the arguments is then getting invoked with the return value of module2, which is
undefined
. With that understanding, let’s go back to the original example, where there was a TypeError:Conclusion The
(function{})();
and(function(){}());
IIFEs can act differently in the missing semicolon situation.Use linter or a tool to make sure modules aren’t missing trailing semicolons when working on modules.
To be extra safe add a leading semicolon to the IIFE:
Hope that helps!
Cool! help me a lot