块级作用域绑定


let声明

声明变量

let a;
let b,c,d;
let e = 100;
let f = 521 , g = 'Iloveyou'

1.变量不能重复声明

let star = '罗志祥'
let star = '小猪'

2.块级作用域

全局,函数,eval

if else while for

{
    let girl = '周扬青'
}
console.log(girl)    //undefined
{
    var girl = '周扬青'
}
console.log(girl)    //周扬青

3.不存在变量提升

{
    console.log(song)
    var song = '9999'    //不会报错,会显示undefined
}
{
    console.log(song)
    let song = '8999'   //报错,无提升
}

4.不影响作用域链

{
    let school = '尚硅谷'
    function fn(){
        console.log(school)
    }
    fn();           //输出尚硅谷
}

const 定义常量

声明常量

const scool = '山硅谷'
console.log(school)

1.一定要赋初始值

const A     //报错,未赋予初始值

2.一般常量使用大写(潜规则)

const a = 100

3.常量的值不能修改

const scool = '山硅谷'
school = 'aaaa'    //报错

4.块级作用域

{
    const Player = 'uzi'
}
console.log(Player)    //报错

5.对于数组和对象的元素修改,不算做对常量的修改,不会报错

const Tanm = ['uzi','mxlg']
Tanm.push('M')

文章作者: Daniel Lin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Daniel Lin !
  目录