I've run across some strangeness in JavaScript syntax that I don't understand.
I was trying to use a single anonymous global function for global abatement, like this:
<script type="text/javascript">
function() {
alert("all code goes here");
}();
</script>
Unfortunately, I get a syntax error. This fixes it:
<script type="text/javascript">
var MAIN = function() {
alert("all code goes here");
}();
</script>
But is unsatisfying because there is now a global MAIN
object. Then I ran across this:
<script type="text/javascript">
(function() {
alert("all code goes here");
})();
</script>
A colleague of mine saw this, shook his head and said "that's some syntax man".
What is going on with
var x = function() { .. }();
that requires parenthesis without the variable like this
(function() { ... })();
Edit: Identical to another question, with this great answer.