admin管理员组

文章数量:1415460

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?

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?

Share Improve this question edited Mar 19, 2012 at 19:48 user166390 asked Mar 19, 2012 at 19:31 cubetwo1729cubetwo1729 1,5161 gold badge11 silver badges18 bronze badges 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;. – maerics Commented Mar 19, 2012 at 19:44
Add a ment  | 

2 Answers 2

Reset to default 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;
    })();
}

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;
  })();
};

本文标签: