Javascript词法分析器是如何解析的?(没有父元素的闭包模式)

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

本文关键字:元素 闭包 模式 词法分析器 何解析 Javascript      更新时间:2023-09-26

我注意到在父级()中包装闭包的这种模式

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

它提出了一个问题,这应该如何被解析:

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

函数周围的圆括号是必要的,因为如果省略,编译器会认为它是一个function Statement。

函数语句不可调用。