ES6(2) - let

let命令:spa

1) 基本用法: ≈ var 區別:聲明的變量僅在let命令所在代碼塊內有效:code

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

2) 不存在變量提高:blog

<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)  暫時性死區:ip

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

4)  模塊內不容許重複定義變量。io

相關文章
相關標籤/搜索