Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created October 24, 2011 03:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilmoore/1308294 to your computer and use it in GitHub Desktop.
Save wilmoore/1308294 to your computer and use it in GitHub Desktop.
IIFE Variations (JavaScript)
// from: http://jsfiddle.net/SubtleGradient/Qfawx/
// copied here only because "gists" are slightly easier for me to read
// classic (oldschool)
(function(){/*IIFE*/})();
// new standard
(function(){/*IIFE*/}());
// lol, square
[function(){/*IIFE*/}()];
// fixed for ASI edge-case
;(function(){/*IIFE*/})();
;(function(){/*IIFE*/}());
;[function(){/*IIFE*/}()];
// Force function to be an expression by setting a var to its result
var stuff = function(){/*IIFE*/}()
// ... this is much more obvious. The extra () makes it obvious that it's not a normal function
var stuff = (function(){/*IIFE*/}())
// without the () it's not obvious that it's an IIFE until you read to the end of the last }, That's annoying!
var doStuff = function(){/*normal function*/}
// Force function to be an expression using an operator
// Don't do this, it's silly and too many characters
"" + function(){/*IIFE*/}()
7 && function(){/*IIFE*/}()
0 || function(){/*IIFE*/}()
7, function(){/*IIFE*/}()
// Minimal
// unconventional and confusing to teh n00bz :(
// pretty hawt though, amirite?
!function(){/*IIFE*/}()
-function(){/*IIFE*/}()
+function(){/*IIFE*/}()
@kevincennis
Copy link

typeof function(){/IIFE/}()

@Hafthor
Copy link

Hafthor commented Feb 15, 2013

~function(){/IIFE/}()

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