写代码不如去泡妞
Published on 2017-09-02 11:31 in 暂未分类 with 涛哥、

let 和 const

  1. let 不存在变量提升
    console.log(a);
    var a = 10;

    console.log(a); ////Cannot access 'a' before initialization
    let a = 10;



    2. 暂时性死区
    if (true) {
        tmp = "fdsa";
        let tmp;
    }



    3.不允许重复声明
    var a = 1;
    var a = 10;
    console.log(a);
    let a = 10;
    let a = 10; //Identifier 'a' has already been declare

    function fun(a) {
        let a; //Identifier 'a' has already been declare
    }
    fun();

    4 块级作用域
    let n = 5;
    if (true) {
        let n = 10;
        console.log("n1:", n); //10
    }
    console.log("n:", n); //5


    var a = 5;
    if (true) {
        var a = 10;
        console.log("a1:", a); //10
    }
    console.log("a:", a); //10



    const 常量
    const一旦声明变量,就必须立即初始化,不能留到以后赋值
    const a = 10;

    const a = 10;
    a = 20;

    const obj = {};
    obj.a = 10;
    console.log("obj:", obj);

    let a = Symbol("id");
    let b = Symbol("id");
    console.log(a == b);
posted @ 2020-12-15 14:06  写代码不如送外卖  阅读(80)  评论(0编辑  收藏  举报