zoukankan      html  css  js  c++  java
  • ES6 中块的概念

    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不能在外部被使用,
  • 相关阅读:
    D2. Remove the Substring (hard version)(思维 )
    暑假集训
    AcWing:167. 木棒(dfs + 剪枝)
    AcWing:109. 天才ACM(倍增 + 归并排序)
    AcWing:99. 激光炸弹(前缀和)
    B. Interesting Array(线段树)
    Best Reward HDU
    G. Swapping Places
    How many HDU
    GSS4&&花仔游历各国
  • 原文地址:https://www.cnblogs.com/MrZhujl/p/9865666.html
Copyright © 2011-2022 走看看