Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
Last active December 10, 2015 02:19
Show Gist options
  • Select an option

  • Save hughfdjackson/4366781 to your computer and use it in GitHub Desktop.

Select an option

Save hughfdjackson/4366781 to your computer and use it in GitHub Desktop.
Why void is better for iifes.
// This is our first js file. We haven't put a trailing ; on the end
var a = 3
alert(a)
// this is our second file, which we've concated on to the end of the first file to HTTP requests
void function(){
alert('hi')
}()
// because ASI doesn't think that `void` means you want to continue your expression, this works
// This is our first js file. We haven't put a trailing ; on the end
var a = 3
alert(a)
// this is our second file, which we've concated on to the end of the first file to HTTP requests
(function(){
alert('hi')
})()
// because ASI doesn't insert in a ; when it thinks you want to continue your expression, it tries to *execute the return from alert*
// and pass in the function in our second file. D'oh.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment