zheng 5 dias atrás
pai
commit
8a7408f4d7
1 arquivos alterados com 40 adições e 0 exclusões
  1. 40 0
      11.复习/11.原型链继承.html

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

@@ -0,0 +1,40 @@
+<!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>
+        /**
+         * 原型链继承
+         * 实现:
+         * 子类的原型 = 父类的实例
+         * 实例顺着父类的原型链查找父类的属性和方法
+         * 优点:
+         * 写法简单
+         * 缺点:
+         * 父类不能进行传参
+         * 父类中 引用数据类型 共享实例 若发生修改 则全部修改
+        */
+       function Person() {
+        this.name = '图图';
+        this.age = 3;
+        this.list = ['吃饭','睡觉','打豆豆'];
+       }
+       Person.prototype.say = function() {
+        console.log("你好")
+       }
+
+       function Child() {}
+
+       Child.prototype = new Person();
+       let c1 = new Child();
+       let c2 = new Child();
+       c2.list.push("12");
+       console.log(c1,'c',c1.list)
+       console.log(c2,'c',c2.list)
+    </script>
+</body>
+</html>