1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // var a = 10;
- // var a = "abc";
- // b = "123";
- // console.log(window.a);
- // window.document.getElementById
-
- // 不属于顶层对象window
- // let a = 10;
- // console.log(window.a);
- // let声明的变量,不允许重复声明
- // let a = 10;
- // let a = 20;
-
- // 变量提升 将变量定义部分提升到当前作用域的最顶端
- // var a;
- // console.log(a);
- // console.log(b);
- // var a = 10;
- // console.log(a);
- // var a
- // console.log(a);
- // a = 10;
- // 作用域 当前变量可作用的范围称之为作用域
- // var a
- // a = 100;
- // function foo(){
- // var a;
- // console.log(a);
- // a = 20;
- // }
- // foo();
- // 函数声明提升
- // function foo(){
- // console.log("helloworld");
- // }
- // foo();
-
- // console.log(foo)
- // 如果函数和变量同时出现提升那么以函数提升为主(变量的层级要高于函数的层级)
- // console.log(a);
- // function a(){
- // console.log("hello");
-
- // }
- // var a = 10;
- // console.log(a);
- // var a = 10;
- // var a = function(){
- // console.log(123);
-
- // }
- // let 不存在变量提升
- // console.log(a);
- // let a = 10;
- // let 暂时性死区 当前作用域下如果有使用let定义的变量那么在定义之前的位置不可以使用
- // function foo(){
- // console.log(a);
- // let a = 20;
- // }
- // foo();
- // if(true){
- // var a = 10;
- // }
- // function foo(){
- // var a = 20;
- // }
- // foo();
- // console.log(a);
- // 块级作用域使用let定义变量时 只要有大括号{} 就会产生一个作用域名
- if(true){
- let a = 10;
- }
- console.log(a);
- </script>
- </body>
- </html>
|