Skip to content

Instantly share code, notes, and snippets.

@kangax
Forked from dperini/easy_error
Created July 15, 2009 20:41
Show Gist options
  • Save kangax/147965 to your computer and use it in GitHub Desktop.
Save kangax/147965 to your computer and use it in GitHub Desktop.
/* can you spot what is happening here */
/* the alert("X") should never execute */
<script>
onload = function() {
var fn = function() {
alert("X");
}
(function() { })();
}
/*
1) Semicolon after first function expression is missing, so overall expression is equivalent to:
var fn = function(){ alert('X'); }(function(){})();
2) Which is essentially double invocation of a function and an assignment of its result to `fn`:
var fn = function(){ alert('X'); }()();
^^^^
3) an error should be thrown since first invocation returns `undefined`, making and expression equivalent to:
var fn = (undefined)(); // Error
*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment