Does strict mode prohibit statement level function declarations?

前端 未结 1 1859
無奈伤痛
無奈伤痛 2020-12-03 21:41
\"use strict\";

if (true) {
  function foo() {
  }
}

In PhpStorm this code shows an error:

Function statement not at top le

相关标签:
1条回答
  • 2020-12-03 22:45

    Yes, in ES5 they are prohibited (and in strict mode, all implementations throw). See also Kangax' great article for function statements in sloppy mode.

    However, in ES6 they are block-level function declarations with new semantics. See also What are the precise semantics of block-level functions in ES6?. This seems to be what Chrome implements here; foo is not available outside of the if block.

    0 讨论(0)
提交回复
热议问题