NOTE: originally, I thought the issue was caused by something more complex; I see now (and edited the title and sample code) that the only difference is the presence or absence of a semicolon. That may make this a no-brainer to answer, but I was still surprised to see a trailing semicolon matters in this case and not others...
Why does this work:
<script type="text/javascript">
this.someFunc = function () {};
(function () {
console.log("self-invoking function called.")
})();
</script>
but this does not:
<script type="text/javascript">
this.someFunc = function () {}
(function () {
console.log("self-invoking function called.")
})();
</script>
and yet, this does:
<script type="text/javascript">
this.someFunc = function () {}
var someVar = "value";
console.log("someVar is:"+someVar);
</script>
The latter interprets the self-invoking function as undefined
, and therefore cannot evaluate/execute it. Tested on Chrome 13, Firefox 6, and Safari 5 on OSX.