Es 6ブロックレベルの役割ドメイン定義宣言関数

654 ワード

//      ES6   
function f() { console.log('I am outside!'); }

(function () {
  if (false) {
    //         f
    function f() { console.log('I am inside!'); }
  }

  f();
}());
// Uncaught TypeError: f is not a function

ブロックレベルの役割ドメイン内で関数を宣言できます.関数宣言はvarと同様で、グローバル役割ドメインまたは関数役割ドメインのヘッダに昇格します.また、関数宣言は、ブロックレベルの役割ドメインのヘッダにも昇格します.実際には次のコードに相当します.
//      ES6   
function f() { console.log('I am outside!'); }
(function () {
  var f = undefined;
  if (false) {
    function f() { console.log('I am inside!'); }
  }

  f();
}());
// Uncaught TypeError: f is not a function