ES6(2) - let

let命令:

1) 基本用法: ≈ var 区别:声明的变量仅在let命令所在代码块内有效:

 <script type="module">
        {
            var a = 100;
            let b = 200;
        }
        console.log(a);//100
        console.log(b);// b is not defined -- Error
 </script>

2) 不存在变量提升:

<script type="module">
      //ES5:
        console.log("ES5:");
        var a = [];
        for(var i = 0; i<10; i++){
            var c = i;
            a[i] = function () {
                console.log(c);
            }
        }
        a[5]();//9


       //ES6:
        console.log("ES6:");
        var b = [];
        for(var j = 0; j<10; j++){
            let d = j;
            b[j] = function () {
                console.log(d);
            }
        }
        b[5]();//5
</script>

3)  暂时性死区:

 <script type="module">
        var a = 200;
        {
            console.log(a);//undefined
            let a = 100;
            console.log(a);//100
        }
 </script>

4)  模块内不允许重复定义变量。

 

posted @ 2017-07-13 11:24  。娴  阅读(124)  评论(0编辑  收藏  举报