fengchuanyu před 2 dny
rodič
revize
9d00b0b9ce
2 změnil soubory, kde provedl 97 přidání a 8 odebrání
  1. 17 0
      8_ES6/21_构造函数.html
  2. 80 8
      8_ES6/习题讲解_this指向.html

+ 17 - 0
8_ES6/21_构造函数.html

@@ -0,0 +1,17 @@
+<!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>
+        // 构造函数
+        // console.log(Array);
+        function Person() {
+            
+        }
+    </script>
+</body>
+</html>

+ 80 - 8
8_ES6/习题讲解_this指向.html

@@ -56,16 +56,88 @@
         // };
 
         //5、请写出下列代码运行结果
-        var name = "window"
+        // var name = "window"
+        // var obj = {
+        //     name: "obj"
+        // }
+        // setInterval(function () {
+        //     console.log(this.name);//window
+        // }, 300)
+        // setInterval(function () {
+        //     console.log(this.name);//obj
+        // }.call(obj), 300)
+
+        //6、请补全下列代码
+        // function foo() {
+        //     //补全此处代码实现每隔一秒输出 hello world
+        //     // console.log("hello world");
+        //     return function(){
+        //         console.log("hello world");
+        //     }
+        // }
+        // window.setInterval(foo(), 1000);
+
+        // 7、补全下列代码实现 1+2+3+4
+        // function add(c, d) {
+        //     return this.a + this.b + c + d;
+        // }
+        // var obj = {
+        //     a:1,
+        //     b:2
+        // }
+        // add.call(obj,3,4);
+        /*
+          在此补全代码 以两种以上方法实现
+        */
+
+        //8、写出下列输出结果
+        // function f() {
+        //     return this.a;
+        // }
+
+        // var g = f.bind({ a: "azerty" });
+        // console.log(g());//azerty
+
+        // // bind 不可以重复绑定 如果重复绑定 会返回第一个绑定的结果
+        // var h = g.bind({ a: 'yoo' });
+        // console.log(h());//azerty
+
+        // var o = { a: 'loveCoding', f: f, g: g, h: h };
+        // console.log(o.f(), o.g(), o.h());//loveCoding azerty azerty
+
+        // //9、补全下列代码
+        // var o = { prop: 'loveCoding' };
+
+        // function independent() {
+        //     return this.prop;
+        // }
+        // //在此补全代码
+        // o.f = independent.bind(o);
+        // console.log(o.f()); //  loveCoding
+
+        //10、用call 或 apply 实现bind 方法
+        function foo(b,c,d) {
+            console.log(this.a,b,c,d)
+        }
         var obj = {
-            name: "obj"
+            a: "hello"
         }
-        setInterval(function () {
-            console.log(this.name)
-        }, 300)
-        setInterval(function () {
-            console.log(this.name)
-        }.call(obj), 300)
+
+        Function.prototype.bind2 = function(){
+            // arguments 是类数组对象 可以获取所有参数
+            // console.log(arguments);
+            var newObj = arguments[0];
+            // 获取所有参数
+            var args = Array.from(arguments).slice(1);
+            console.log(args);
+            var _this = this;
+            return function(){
+                _this.apply(newObj,args)
+            }
+        }
+
+        var foo2 = foo.bind2(obj,"world","你好","世界");
+        foo2()
     </script>
 </body>