5

In Firefox I received a weird syntax error, as this is not trivial, and an interesting syntax error I'd like to post here because I don't know it's happening.

Should file this as a bug report?

I was testing some scripts from here: here

It gave me a syntax error. SyntaxError: invalid label at line 5.

app.directive("alertable", function()
{
    return 
    {
        restrict : "A",
        link: function(scope, element, attrs) 
        {
            element.bind("click", function() 
            {
                alert(attrs.message);
            });
        }
    };
});

And this one, don't:

app.directive("alertable", function()
{
    return { // fix???
        restrict : "A",
        link: function(scope, element, attrs) 
        {
            element.bind("click", function() 
            {
                alert(attrs.message);
            });
        }
    };
});

1 Answer 1

11

This behavior is by design.

Semicolons in Javascript are optional. (ASI)
The parser inserts an implicit semicolon after the return line, and assumes that the { begins a code block. (like after an if or for)

The first line in that code block is not actually valid code, so you get that error.

This happens because return is a valid statement both with and without an operand.

Similarly, the code

return
4;

Is parsed as return; 4;.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for enlightment, I never heard before about "ASI". Good to know. Now i shall code better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.