e 1 жил өмнө
parent
commit
2a1156ec39

+ 41 - 0
JS高级/23.继承1.0.html

@@ -0,0 +1,41 @@
+<!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>
+      /**
+       * 1.原型链继承:
+       * 构造函数 原型 实例
+       */
+      function Father() {
+        this.name = "Lucy";
+        this.arr = [1, 2, 3];
+      }
+      function Child() {
+        this.age = 18;
+      }
+      Child.prototype = new Father();
+      let newChild = new Child();
+      console.log(newChild.arr,'新的')
+
+      let a1 = new Child();
+      let a2 = new Child();
+
+      /**
+       * 因为两个实例使用的是同一个原型,所以一个修改另一个也会发生改变
+      */
+      a2.arr.push(10);
+      a1.name += 10; //a1.name = a1.name + 10
+      a2.name += 20;
+      console.log(a1,'a1');
+      console.log(a2,'a2');
+
+
+
+    </script>
+  </body>
+</html>

+ 49 - 0
JS高级/24.继承2.0.html

@@ -0,0 +1,49 @@
+<!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>
+        /**
+         * 2.构造函数的继承(call/apply)
+         * 
+        */
+       function Father() {
+          this.name = 'LiLi';
+          this.color = 'red';
+          this.arr = [1,2,3];
+          this.fn1 = function() {
+            console.log("这是第一个方法");
+          }
+       }
+       Father.prototype.getColor = function() {
+          return this.color;
+       }
+       function Child() {
+          Father.call(this);
+       }
+       let newChild = new Child();
+       newChild.fn1();
+       console.log(newChild);
+       let a1 = new Child();
+       let a2 = new Child();
+       a1.name += 10;
+       a2.name += 20;
+       a2.arr.push(10);
+       console.log(a1,'a1');
+       console.log(a2,'a2');
+       /**
+        * 构造函数继承可以解决原型链继承的同时修改引用数据类型的缺点
+        * 可以使用父级构造函数上的属性和方法
+        * 不可以使用父级原型上的属性和方法 
+       */
+       console.log(newChild.getColor());
+
+       
+
+    </script>
+</body>
+</html>