zoukankan      html  css  js  c++  java
  • es6之常/变量以及兼容ES6

    在es5时代声明一个变量或者常量只有var,在es6的时候就出现了变量(let)和常量(const)的具体细分。

    1、变量

      用let进行声明变量,更严谨更好使。
      特点:1、不能进行变量提升
           2、不能重复定义同一个变量
           3、不能定义函数的参数。
           4、块级作用域
    
    `//1、不能重复声明同一个变量名
    // var num;
    // let num = 2; //dentifier 'num' has already been declared
    // console.log(num);
    
    //2、不能定义声明函数参数
    
    // function fn1(age) {
    //     let age = 12;
    //     console.log(age); //Identifier 'age' has already been declared
    // }
    // fn1(34);
    
    //3、不能进行变量提升
    // function fn2() {
    //     console.log(num);
    //     let num = 13;  //Cannot access 'num' before initialization
    // }
    // fn2();
    
    //4、块级作用域
    for (var index = 0; index < 5; index++) {
        // var num = 25;
        let num = 26;
    }
    console.log(num); //num is not defined`
    

    2、常量

      用const进行定义,特殊之处就是不可以修改值。
      特点:1、常量声明后需要进行赋值
           2、不能进行修改
           3、不能进行提升
           4、不能重复定义一个常量名
           5、块级作用域
    
    //1、常量声明需要赋值
    // const num = 1;
    // const num;  //Missing initializer in const declaration
    // console.log(num);
    
    // 2、不能重复定义常量
    // const num = 1;
    // const num = 2;   //Identifier 'num' has already been declared
    // console.log(num);
    
    //3、块级作用域
    
    // for (var i = 0; i < 5; i++) {
    //     const num = 2;
    // }
    // console.log(num);  // num is not defined
    
    //4、不能进行提升
    
    // function fn1() {
    //     console.log(num);  //Cannot access 'num' before initialization
    //     const num = 2;
    // }
    // fn1();
    
    //5、不能修改    
    // const num = 2;
    // num = 3;  // Assignment to constant variable.
    // console.log(num);
    

    3、如何兼容ES6

      使用babel。  引入babel.js, 同时在script标签内 加上type="text/babel"
      <script src="./node_modules/babel-core/browser.min.js"></script>
      <script type="text/babel">
          const num = 25;
          alert(num);
      </script>
  • 相关阅读:
    XDebug的配置和使用
    PHP一致性hash
    命令注入绕过技巧总结
    Aireplay-ng 6 种常用攻击模式详解
    CDlinux无线审计工具使用
    Aircrack-ng无线审计工具使用
    Ubuntu中的mysql
    Centos安装python3.7时遇到的问题
    写程序的时候发现了个数学在线工具,感觉挺好,Gegebra
    OpenCV实现图像变换(python)-仿射变换原理
  • 原文地址:https://www.cnblogs.com/qianqiang0703/p/13591158.html
Copyright © 2011-2022 走看看