1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ericsoco
  • 24,913
  • 29
  • 97
  • 127
  • 1
    possible duplicate of [Best practice for semicolon after every function in javascript?](http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript) – Alex Wayne Sep 02 '11 at 18:11
  • yes, i think so. sorry. – ericsoco Sep 02 '11 at 18:18

2 Answers2

2

I believe the second snippet is actually executing the empty function declaration. If you change your example to this:

<script type="text/javascript">
    this.someFunc = function (arg) { console.log(arg); }

    (function () {
        console.log("self-invoking function called.")
    })();
</script>

and then run (FF6), you will see that it logs: function(). It's passing in the second anonymous function as an argument for the first. This makes more sense if you rearrange:

<script type="text/javascript">
    this.someFunc = function (arg) { console.log(arg); }(
        function () {
        console.log("self-invoking function called.")
    })

    (); //the parser doesn't know what to do with this line.
</script>
InvisibleBacon
  • 3,137
  • 26
  • 27
  • yep, makes sense, and is in line with the other leading question (http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript). thx bacon. – ericsoco Sep 02 '11 at 18:26
0

Just looking at the first Related topic on the right of your question: Why should I use a semicolon after every function in javascript?

The accepted answer is a great explanation of this behavior.

Community
  • 1
  • 1
PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • oof. the semicolon-related posts didn't come up as related until after i posted this, since my post didn't even address semicolons until i figured that part out...right before i posted it. sorry to duplicate. i'll choose this answer once the 5-minute time minimum time is up... – ericsoco Sep 02 '11 at 18:17
  • and nice to see you this side of the processing forums, philho! – ericsoco Sep 02 '11 at 18:17
  • Well, I wasn't aware of the issue, so thanks for pointing it out... And the explanation of @InvisibleBacon is detailed and somehow clearer than the one I point to (which is good, still), so I suggest to give him the accepted answer, mine is more a pointer than a real answer. – PhiLho Sep 02 '11 at 22:23