Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created December 24, 2010 00:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cowboy/753725 to your computer and use it in GitHub Desktop.
Save cowboy/753725 to your computer and use it in GitHub Desktop.
A few variations on writing an IIFE
// Want to learn more about Immediately Invoked Function Expressions? Read
// this article first:
//
// http://benalman.com/news/2010/11/immediately-invoked-function-expression/
// Parens are used to tell the parser that it's a function expression,
// not a function declaration. If you don't explicitly tell the parser to
// expect an expression, it will throw a SyntaxError exception because it
// sees a function declaration without a name specified.
(function(){ /* code */ })() // I've been using this one
(function(){ /* code */ }()) // Crockford recommends this one
// If you don't care about the possible performance overhead of performing
// type coercion on the function's return value, you can save a byte by
// prefixing the function with a unary operator.
!function(){ /* code */ }()
~function(){ /* code */ }()
-function(){ /* code */ }()
+function(){ /* code */ }()
// Because the point of the parens or coercing operators is just to tell
// the parser to expect a function expression and not a declaration, they
// can be omitted when it already expects an expression.
var i = function(){ return 10; }();
condition && function(){ /* code */ }();
// Here's another variation, from @kuvos - I'm not sure of the performance
// implications of using the `new` keyword, but it works.
// http://twitter.com/kuvos/status/18209252090847232
new function(){ /* code */ }
new function(){ /* code */ }() // Only need parens if passing arguments
@DmitrySoshnikov
Copy link

a SyntaxError exception because it sees a function declaration without a name specified

A missing name is only one case of this problem.

function foo() {
  ...
}();

Also a syntax error, but we have the name now.

The thing is that an expression statement cannot start with a function keyword since then it's ambiguous with a function declaration. But in the global scope, the parser should treat such a function as declaration. In case above we have two productions: a function declaration and a grouping operator which causes the syntax error.

Details: http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses

Dmitry.

@cowboy
Copy link
Author

cowboy commented Dec 31, 2010

Thanks for the comment, Dmitri. I've actually just updated/rewritten my original article to reflect this and other changes I've wanted to make.

Check it out if you have any interest!
Immediately-Invoked Function Expression

@DmitrySoshnikov
Copy link

Yep, great; also I like an IIFE abbreviation. It's seems the most fitted naming IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment