<!-- <script> window.onload = function () { var a = 10; let b = 20; const c = 30; // ES5 预解析 变量提升 var function 结果 undefined console.log(x); var x = 11; // ES6 中 直接报错 //--------------------------------------------------------------------- var aa = 11; var aa = 22; console.log(aa); // ES5不报错 aa=22; let bb = 22; //let bb=33; //ES6直接报错了,同一个作用域 不能出现相同的变量名称 //----------------------------------------------------------------------- //ES6出现块级作用域 function ff() { var cc = 111; if (true) { var cc = 999; } console.log(cc) //999 } function fff() { let cc = 111; if (true) { let cc = 999; console.log(cc) //999 } console.log(cc) //111 } //------------------------------------------------- function ddd1(i) { var i = 1000; console.log(i) // 1000 参数名 和变量名重了 var可以 let不行 } ddd1(88); function ddd(i) { // let i = 1000; console.log(i) // Identifier 'i' has already been declared 参数名 和变量名重了 } ddd(99); ff(); fff(); //----------------------------------------------- for (var i = 0; i < 5; i++) { } console.log(i) // 5 var 把i 提升为全局变量了 相当于var i=1 ; var i=2... var i=5; for (let i = 0; i < 5; i++) { } console.log(i) // Identifier 'i' not fund var arr = []; for (let i = 0; i < 5; i++) { arr[i] = (() => { console.log(i) }) } arr[3]() // var 都为5 改为let 则为对应的数字 arr[1]() } </script> -->
标签:function,ES6,console,log,cc,let,var From: https://www.cnblogs.com/soulice/p/16707236.html