Why does this cause an "invalid syntax" error with the Google Closure compiler?

If the following is submitted to Google Code Closure:

return (function() { return true; })(); 

he says that there is a parsing error due to invalid syntax. What could be the problem?

+4
source share
2 answers

If this is all your code, the problem is that you cannot have a return (first) outside the function definition. Try:

 function foo() { return (function() { return true; })(); } 
+4
source

The problem is that you are using return as a top-level construct (outside of any function body). You need to wrap it in a context in which return valid:

 var example = function () { return (function() { return true; })(); }; 
+3
source

Source: https://habr.com/ru/post/1402299/


All Articles