开发者

How is this parsed by the Javascript lexer? (Closures-pattern without parens)

开发者 https://www.devze.com 2023-03-02 05:28 出处:网络
I\'ve noticed this pattern of wrapping closures in parens () (function () 开发者_开发百科{ var foo = 1;

I've noticed this pattern of wrapping closures in parens ()

(function () 开发者_开发百科{
  var foo = 1;
  return function () {return foo}
}())

It raises the question, how is this supposed to be parsed:

function () {
  var foo = 1;
  return function () {return foo}
}()


The parentheses around the function are necessary, because when omitted, the compiler assumes it is a Function Statement.

Function statements are not invocable.

0

精彩评论

暂无评论...
验证码 换一张
取 消