Search code examples
javascriptaptana3

javascript: Why does this return statement cause a syntax error?


I'm using Apatana 3, i modified the JS code formatter a little bit to let it seem more clear, below is the code after format, it give me an error:

    copyOffset : function( index )
    {
        return
        {
            x : index, y : index
        };
    }

firebug give me:

SyntaxError: invalid label

if i change it to:

    copyOffset : function( index )
    {
        return{
            x : index, y : index
        };
    }

will be OK, Anybody who can tell me what's the diff between these two return statement?


Solution

  • The difference is that the first snippet is actually interpreted as...

    copyOffset : function( index )
    {
        return;
        {
            x : index, y : index
        };
    }
    

    It's called Automatic Semicolon Insertion: when JavaScript parser sees a statement that seems to be complete, but misses semicolon, it attempts to 'fix' it.

    And yes, even though helpful at times, it can be quite annoying. This article explains this JavaScript feature in details.