fengchuanyu 3 月之前
父节点
当前提交
c95db74ef0
共有 2 个文件被更改,包括 141 次插入0 次删除
  1. 94 0
      1_新的声明方法.html
  2. 47 0
      2_块级作用域.html

+ 94 - 0
1_新的声明方法.html

@@ -0,0 +1,94 @@
+<!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);
+
+        if(true){
+            let a = 10;
+        }
+        console.log(a);
+    </script>
+</body>
+</html>

+ 47 - 0
2_块级作用域.html

@@ -0,0 +1,47 @@
+<!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>
+    <ul>
+        <li>1</li>
+        <li>2</li>
+        <li>3</li>
+        <li>4</li>
+        <li>5</li>
+        <li>6</li>
+    </ul>
+    <script>
+        var aLi = document.getElementsByTagName('li');
+        for(let i=0;i<aLi.length;i++){
+            aLi[i].index = i;
+            aLi[i].onclick = function(){
+                console.log(i);
+            }
+        }
+        
+        // var a = 10;
+        // function foo(){
+        //     console.log(++a);
+        // }
+
+        // foo();
+        // foo();
+        // 闭包 当出现多重作用域嵌套时候,内部函数调用外部函数的局部变量 变量就不会被释放
+        function foo(){
+            var a = 10;
+            function foo2(){
+                console.log(++a);
+            }
+            return foo2;            
+        }
+
+        var f = foo();
+        f();
+        f();
+    </script>
+</body>
+</html>