zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    关于Django
    Django
    如何在六个月掌握一门外语
    基础术语
    机器学习
    2018-01-05 通用型的中文编程语言探讨之一: 高考
    2018-01-04 浅尝The Little Prover一书, 重逢Chez Scheme
    2018-01-03 烂尾工程: Java实现的汇编语言编译器
    2018-01-03 中文编程专栏月报:2017年12月
    2017-12-26 Java关键字的汉化用词探讨
  • 原文地址:https://www.cnblogs.com/chengxiao35/p/13598647.html
Copyright © 2011-2022 走看看