4

If the following is passed into Google code closure:

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

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

1
  • 1
    Note that you are not returning an anonymous function but creating and calling an anonymous function. So this statement is equivalent to just return true;. Commented Mar 19, 2012 at 19:44

2 Answers 2

4

If that is your entire code, the problem is that you can't have a return statement (the first one) outside a function definition. Try:

function foo() {
    return (function() {
        return true;
    })();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. You can use the compiler's output wrapper if you are trying to optimize an expression. Something like: --output_wrapper="return %output%"
3

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

var example = function () {
  return (function() {
    return true;
  })();
};

Comments

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.