为什么函数被描述为块作用域

Max*_*kyi 3 javascript ecmascript-6

在ES6上这本书,有以下内容:

功能声明......

  • 像块一样是块状的.
  • 在全局对象中创建属性(在全局范围内),如var.
  • 被提升:独立于其范围中提到的函数声明的位置,它始终在范围的开头创建.

AFAIK,函数一直是函数作用域.我认为ES6中的某些内容可能已经发生了变化,但是没有:

function a() {
    if (true) {
        // defined inside the block and is hoisted to the top of that block
        z();
        function z() { console.log ('z')}
    }

    z();
}

// but is also hoisted to the function scope
a(); // works OK
Run Code Online (Sandbox Code Playgroud)

实际上,它们似乎是块范围的:

function a() {
    if (false) {
        // defined inside the block and is hoisted to the top of that block
        z();
        function z() { console.log ('z')}
    }

    z(); // error
}
Run Code Online (Sandbox Code Playgroud)

那么它在ES6中有所改变吗?

T.J*_*der 5

AFAIK,函数一直是函数作用域.我觉得ES6可能会有所改变

确实如此:在ES2015之前,该规范并未涵盖块内声明的功能.支持它们是允许的扩展,但不是规范的一部分.

因此,规范必须跳过箍,特别是在浏览器的松散模式下.

严格模式下,您将在兼容引擎上找到函数声明确实是块作用域的:

"use strict";

function test() {
  if (true) {
    function foo() {
      console.log("foo called");
    }
  }
  try {
    foo(); // ReferenceError
  } catch (e) {
    console.log("Error: " + String(e));
  }
}
test();
Run Code Online (Sandbox Code Playgroud)

在兼容的JavaScript引擎(例如任何最新版本的Chrome中的V8,或任何最新版本的Firefox中的SpiderMonkey)中,您将获得ReferenceError上述内容.