block scoped

Block Scoped

Block-Scoped Variables

宣告一個可隨意更改其內容的區域變數(block scope)

if(true){
  let a = 1;
  console.log(a);
}
console.log(a);
var funcs = [];

for (let i = 0; i < 5; i++){
  funcs.push(function(){
    console.log(i);
  });
}

funcs[3](); // 3

Block-Scoped Functions

ES6

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

ES5

(function () {
    var foo = function () { return 1; }
    foo() === 1;
    (function () {
        var foo = function () { return 2; }
        foo() === 2;
    })();
    foo() === 1;
})();

results for ""

    No results matching ""