fengchuanyu 4 ماه پیش
والد
کامیت
61c8b677ef
2فایلهای تغییر یافته به همراه170 افزوده شده و 0 حذف شده
  1. 76 0
      6_ES6/3_立即执行函数.html
  2. 94 0
      6_ES6/练习题1_函数及变量特性.html

+ 76 - 0
6_ES6/3_立即执行函数.html

@@ -0,0 +1,76 @@
+<!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 (var i = 0; i < aLi.length; i++) {
+            // aLi[i].index = i;
+            // aLi[i].onclick = function () {
+            //     console.log(this.index);
+            // }
+            (function(a){
+                aLi[a].onclick = function(){
+                    console.log(a);
+                }
+            })(i);
+        }
+
+        // var foo = function(){
+        //     console.log("hello");
+        // }
+
+        // foo();
+        // console.log(foo);
+
+        // 立即执行函数
+        // (function(){
+        //     console.log("hello");
+        // })();
+
+        // var a = 20;
+        // function foo(){
+        //     var a = 10;
+        //     function foo2(){
+        //         // var a = 30;
+        //         console.log(a++);
+        //     }
+        //     // foo2();
+        //     // foo2();
+        //     return foo2;
+        // }
+        // var foo3 = foo();
+        // // console.log(foo3)
+        // foo3();
+        // foo3();
+
+
+
+
+
+        // 1. 为了防止变量名冲突
+        // 2. 为了防止全局变量污染
+
+
+
+
+
+    </script>
+</body>
+
+</html>

+ 94 - 0
6_ES6/练习题1_函数及变量特性.html

@@ -0,0 +1,94 @@
+<!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 n = 13;
+        function fn(n) {
+            alert(n);//
+            n = 14;
+            alert(n);
+        }
+        fn(n);
+        alert(n)
+
+        // 第二题
+        var n = 0;
+        function a() {
+            var n = 10;
+            function b() {
+                n++;
+                alert(n);
+            }
+            b();
+        }
+        a();
+        alert(n);
+
+        // 第三题
+        console.log(num, str);
+        var num = 18;
+        var str = "lily";
+        function fn2() {
+            console.log(str, num);
+            num = 19;
+            str = "candy";
+            var num = 14;
+            console.log(str, num);
+        }
+        fn2();
+        console.log(str, num);
+
+        // 第四题
+        fn();
+        function fn() { console.log(1) };
+        fn();
+        var fn = 13;
+        fn();
+        function fn() { console.log(2) };
+        fn();
+
+        // 第五题
+        (function f() {
+            function f() { console.log(1) };
+            f();
+            function f() { console.log(2) };
+        })();
+
+        // 第六题
+        if (!("a" in window)) {
+            var a = 10;
+        }
+        alert(a);
+        console.log(fn);
+        if (9 == 8) {
+            function fn() {
+                alert(2);
+            }
+        }
+        
+        // 第七题
+        function fn() {
+            var i = 5;
+            return function (n) {
+                console.log(n * i++);
+            }
+        }
+        var f = fn();
+        f(4);
+        fn()(5);
+        f(6);
+
+
+    </script>
+</body>
+
+</html>