严格模式是否禁止语句级函数声明? [英] Does strict mode prohibit statement level function declarations?

查看:179
本文介绍了严格模式是否禁止语句级函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"use strict";

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

在PhpStorm中,此代码显示错误:

In PhpStorm this code shows an error:


函数语句不在程序的顶层或者禁止使用功能

Function statement not at top level of a program or function is prohibited

然而,Chrome很乐意执行它,即使在调试器中也没有任何控制台输出。

However, Chrome happily executes it, even in the debugger and without any console output.

现在是否禁止?

推荐答案

是的,在ES5中它们被禁止(和在严格模式下,所有实现抛出)。另请参阅 Kangax的精彩文章,了解草率模式下的函数语句。

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

但是,在ES6中,它们是具有新语义的块级函数声明。另请参见 ES6中块级功能的精确语义是什么?。这似乎是Chrome在这里实现的; foo if 块之外不可用。

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.

这篇关于严格模式是否禁止语句级函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆