1. 程式人生 > >ES6 中塊的概念

ES6 中塊的概念

聲明 fin n) for 函數 true 花括號 是個 UNC

ES6中新增了塊的概念

塊:

  是個花括號 { }

  常用的一些帶{ }的地方:

1  if(){}
2  for(){}
3  while(){}                  
4  switch(){}
5  function fn(){}

用了塊{ },產生的變化:

1.沒有被塊 包著的函數聲明,在全局都能被訪問到

1 console.log(fn);//可以被訪問到,是個函數代碼塊
2     function fn() {
3 }
4 console.log(fn);//也可以被訪問到,是個函數代碼塊

2.被{塊}包住的函數聲明,在 { }上方訪問時undefined / let 和 const 聲明的變量 和 常量 支持{ }的概念,在塊之外不能被訪問(使用),

 1     console.log(a);//undefined
 2     if(true){
 3     var a = 1;
 4         console.log(a);//1
 5     }
 6     console.log(a);//條件是true:是1  。條件是false:是undefined
 7 /*******************************************************************/
 8     console.log(fn);//不管條件是否成立,在塊的上方訪問都為undefined
 9     if(true){
10 function fn() { 11 } 12 } 13 console.log(fn);//條件是true:是函數代碼塊 。條件是false:是undefined 14 /*******************************************************************/ 15 //console.log(b);報錯 16 { 17 let b = 0; 18 console.log(b);//0 19 } 20 console.log(b);//報錯 塊 中的let不能在外部被使用,

ES6 中塊的概念