12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // var a = 99; //全局变量
- // f();
- // console.log(a); //输出全局的变量
- // function f() {
- // console.log(a); // undefined 因为var有变量提升
- // var a = 10; //在局部声明
- // console.log(a); // 10
- // }
- // 输出结果:
- /*
- undefined 10 99
- */
- // let a = 'aaa';
- // console.log(a);
- // console.log(window.a);//undefined
- // var b = 'bbb';
- // console.log(b);
- // console.log(window.b);
- // c = 'ccc';
- // console.log(c);
- // console.log(window.c);
- function a() {
- aa = 'aaaa';
- }
- a();
- console.log(aa);
- // function b() {
- // var bb = 'bbbb';
- // }
- // b();
- // // console.log(bb);
- // function c() {
- // let cc = 'cccc';
- // }
- // c();
- // console.log(cc);
- /* for 设置循环变量的部分 是一个父作用域 */
- /* 循环体内 是一个单独的子作用域 */
- // for (let i = 0; i < 3; i++) {
- // let i = 'aaa';
- // console.log(i);
- // }
- // for (var j = 0; j < 3; j++) {
- // var j = 'bbb';
- // console.log(j);
- // }
- /*
- x
- aaa bbb
- aaa aaa aaa bbb
- x bbb
- aaa bbb
- */
- for (let i = 0; i < 10; i++) {
- console.log(i);// 0 1 2 3 4 5 6 7 8 9
- setTimeout(function () {
- console.log(i);
- }, 0);
- }
- </script>
- </body>
- </html>
|