e 1 yıl önce
ebeveyn
işleme
1cd0f5391c
2 değiştirilmiş dosya ile 73 ekleme ve 0 silme
  1. 32 0
      JS高级/25.继承3.0.html
  2. 41 0
      JS高级/26.继承4.0.html

+ 32 - 0
JS高级/25.继承3.0.html

@@ -0,0 +1,32 @@
+<!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];
+            this.fn1 = function() {
+                console.log("这是组合继承");
+            }
+        }
+        Father.prototype.getName = function() {
+            return this.name;
+        }
+        function Child() {
+            Father.call(this);
+            this.color = 'red';
+        }
+        Child.prototype = new Father();
+        Child.prototype.constructor = Child;
+        var news = new Child();
+        console.log(news);
+
+    </script>
+</body>
+</html>

+ 41 - 0
JS高级/26.继承4.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>
+      /**
+       * 原型式继承
+       * ES5提供
+       * Object.create(必传项(新原型对象),选传项(新原型对象的属性))
+       * 用于浅拷贝
+       * 浅拷贝与深拷贝:
+       *    浅拷贝:被引用出来的地址发生改变,原地址也改变
+       *    深拷贝:被引用出来的地址发生改变,原地址不改变
+       */
+    let Father = {
+        name:'小红',
+        arr:[1,2,3],
+        fn1:function(){
+            console.log("这是原型式继承");
+        }
+    }
+      let Child = Object.create(Father);
+      Child.name = '小明';
+      Child.arr.push(10);
+
+      let Child1 = Object.create(Father);
+      Child1.name = '小绿';
+      Child1.arr.push(20);
+
+      console.log(Child,'Child');
+      console.log(Child1,'Child1');
+
+    
+
+    </script>
+  </body>
+</html>