ECMAScript 6 新特性之-函數塊級作用域

最近更新時間 2020-01-10 13:48:19

ES6 中函數塊級作用域有新的寫法,跟 ES5 的差別如下:

//ES6
{
  function foo () { return 1 }
  foo() === 1
  {
    function foo () { return 2 }
    foo() === 2
  }
  foo() === 1
}



//  only in ES5 with the help of block-scope emulating
//  function scopes and function expressions
(function () {
  var foo = function () { return 1; }
  foo() === 1;
  (function () {
    var foo = function () { return 2; }
    foo() === 2;
  })();
  foo() === 1;
})();

 

rss_feed