e 1 yıl önce
ebeveyn
işleme
b4b5c05e4d

+ 37 - 0
JS高级/27.继承5.0.html

@@ -0,0 +1,37 @@
+<!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>
+      /**
+       * 寄生式继承
+       *    基于原型式继承 就是在原型式继承的基础上添加方法
+       */
+      let father = {
+        name: "LiLi",
+        age: 18,
+        getColor: function () {
+          return this.age;
+        },
+      };
+
+      function Way(methods) {
+        let obj = Object.create(methods);
+        obj.getName = function() {
+            return this.name;
+        }
+        return obj;
+      }
+
+      let child = Way(father);
+
+      console.log(child.getColor());
+      console.log(child.getName());
+
+    </script>
+  </body>
+</html>

+ 42 - 0
JS高级/28.继承.6.0.html

@@ -0,0 +1,42 @@
+<!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 Father() {
+        this.name = "小明";
+        this.arr = [1, 2, 3];
+      }
+      Father.prototype.getName = function () {
+        return this.name;
+      };
+      function Child() {
+        Father.call(this);
+        this.color = "red";
+      }
+      function Way(father,child) {
+        child.prototype = Object.create(father.prototype);
+        child.prototype.constructor = child;
+      }
+      Way(Father,Child);
+      
+      Child.prototype.getColor = function() {
+        return this.color;
+      }
+
+      let a1 = new Child();
+      console.log(a1.getName(),'a1.getName');
+      console.log(a1.getColor(),'a1.getColor');
+
+
+    </script>
+  </body>
+</html>

+ 41 - 0
JS高级/29.继承7.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>
+        class Vase {
+            constructor(name) {
+                this.name = name;
+            }
+            music() {
+                console.log("音乐课");
+            }
+        }
+
+        class Year extends Vase {
+            constructor(name,age) {
+                super(name);
+                this.age = age;
+            }
+            sport() {
+                console.log("运动");
+            }
+            static run() {
+                console.log("跑步")
+            }
+        }
+
+        let a1 = new Vase("小明");
+        let a2 = new Year("小红",18);
+
+        console.log(a1,'a1');
+        console.log(a2,'a2');
+        console.log(Year.run());
+
+    </script>
+</body>
+</html>