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