Created
October 24, 2011 03:20
-
-
Save wilmoore/1308294 to your computer and use it in GitHub Desktop.
IIFE Variations (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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*/}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~function(){/IIFE/}()