zheng 5 napja
szülő
commit
57bfbe207c

+ 1 - 0
11.复习/11.原型链继承.html

@@ -35,6 +35,7 @@
        c2.list.push("12");
        console.log(c1,'c',c1.list)
        console.log(c2,'c',c2.list)
+       c1.say()
     </script>
 </body>
 </html>

+ 47 - 0
11.复习/12.构造函数继承.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>
+    <script>
+        /**
+         * 构造函数继承
+         * 实现:
+         *  在子类的构造函数中 用call/apply把父类的this指向修改 指向子类的实例
+         * 优点:
+         * 父类可以传参
+         * 父类的引用数据类型 不同享实例
+         * 缺点:
+         * 父类原型上的方法 没有被继承
+        */
+        function Person(x) {
+            this.address = x;
+            this.name = '图图';
+            this.age = 3;
+            this.list = ['吃饭', '睡觉', '打豆豆'];
+        }
+        Person.prototype.say = function () {
+            console.log("你好")
+        }
+
+        function Child(val) {
+            console.log(val,'val')
+            // Person.call(this,val)
+            Person.apply(this,[val])
+         }
+
+        let c1 = new Child('北京');
+        let c2 = new Child('哈尔滨');
+        c2.list.push("12");
+        console.log(c1, 'c1', c1.list)
+        console.log(c2, 'c2', c2.list)
+        c1.say()
+    </script>
+</body>
+
+</html>