Why does this cause a syntax error for the return statement:
var FOO = (function($)
{
return
{
init: function()
{
}
}
})(jQuery);
Whereas this doesn't:
var FOO = (function($)
{
return {
init: function()
{
}
}
})(jQuery);
Why is there a difference?
It's not about the whitespace, it's about automatic semicolon insertion by JavaScript.
ECMAScript specification says
This means that your code
gets translated as
So FOO will be undefined after the function is executed.
For your other code,
JS won't insert any semicolon and it will work correctlyJS will insert semicolon after your literal object and it should work fine. [EDIT: Correction as pointed out by kagnax]This means that you should always terminate your statements with semicolon. Letting JS engine insert semicolon can introduce very subtle bugs, which would take hours to debug. You can use a tool like JSLint which will warn you of missing semicolons.