面向對象的詳細理解與繼承方法對比

在這裏插入圖片描述
類的聲明

/**
       *es5 
       */
      var Animal = function () {
          this.name = 'Animal';
      };

      /**
       * es6中class的聲明
       */
      class Animal2 {
          constructor () {
              this.name = 'Animal2';
          }
      }

      /**
       * 實例化
       */
      console.log(new Animal(), new Animal2());

在這裏插入圖片描述
1. 藉助構造函數實現繼承
原理:在子類裏面執行父級的構造函數,將父級的構造函數的this指向子類構造函數的實例中去。
由於構造函數有自己的原型鏈,這種繼承方式的缺點是父級的原型鏈上的屬性並沒有被子類所繼承

   function Parent1 () {
       this.name = 'parent1';
   }
   Parent1.prototype.say = function () {
     console.log("say hi")
   };
   function Child1 () {
       //在子類裏面執行父級的構造函數,將父級的構造函數的this指向子類構造函數的實例中去
       Parent1.call(this);//apply
       this.type = 'child1';
   }
   console.log(new Child1());
   console.log(new Child1().say());

在這裏插入圖片描述
看打印結果Child1並沒有say()這個方法

2. 藉助原型鏈實現繼承
將父級的構造函數賦給子類構造函數的原型對象

      function Parent2 () {
          this.name = 'parent2';
          this.play = [1, 2, 3];
      }
      function Child2 () {
          this.type = 'child2';
      }
      
      Child2.prototype = new Parent2();
      console.log(new Child2());
      new Child2().__proto__===Child2.prototype
	  // true
	  new Child2().__proto__.name
	  // "parent2"

在這裏插入圖片描述
實例化出的子類的_proto_會等於子類構造函數的原型對象,即父類的實例對象:new Parent2()
接下來看:

      var s1 = new Child2();
      var s2 = new Child2();
      //改變s1的屬性
      s1.play.push(4);
      console.log(s1.play, s2.play);

在這裏插入圖片描述
s1.proto===s2.proto
// true
缺點是:改一個實例對象的屬性另一個也會跟着變,因爲在原型鏈中兩個子實例化對象的原型對象是共用的

3.組合方式:構造函數+原型鏈
在子類構造函數中執行父級構造函數,再將父級實例化對象賦給子類構造函數的原型對象

      function Parent3 () {
          this.name = 'parent3';
          this.play = [1, 2, 3];
      }
      function Child3 () {
          Parent3.call(this);
          this.type = 'child3';
      }
      Child3.prototype = new Parent3();
      var s3 = new Child3();
      var s4 = new Child3();
      s3.play.push(4);
      console.log(s3.play, s4.play);
      console.log(s3.constructor);
      //function Parent3() {
          this.name = 'parent3';
          this.play = [1, 2, 3];
      }

在這裏插入圖片描述
組合方式: 缺點是實例化子類的時候,父級的構造函數執行了兩次,new Child3()的時候執行了一次,在原型鏈上new Parent3()又執行了一次。沒有必要

4.組合繼承的優化1
直接把父類的原型對象直接賦給子類的原型對象,這樣就實現了繼承
原型對象在這裏只是引用不會執行,只是在子類實例化時才執行

      function Parent4 () {
          this.name = 'parent4';
          this.play = [1, 2, 3];
      }
      function Child4 () {
          Parent4.call(this);
          this.type = 'child4';
      }
      //原型對象在這裏只是引用不會執行
      Child4.prototype = Parent4.prototype;
      var s5 = new Child4();
      var s6 = new Child4();
      s5.play.push(4);
      console.log(s5, s6);

在這裏插入圖片描述

     console.log(s5 instanceof Child4, s5 instanceof Parent4);
     // true true
     console.log(s5.constructor);
     //function Parent4() {
         this.name = 'parent4';
         this.play = [1, 2, 3];
     }

在這裏插入圖片描述
因爲原型對象公共了,所以無法區分子類的實例化對象到底是誰的直接實例化對象
因爲s5.constractor=Child4 而Child4=Child4.prototype.constractor
因爲Child4.prototype = Parent4.prototype, 所以Child4 = Parent4.prototype.constractor;繼而s5.constractor=Parent4.prototype.constractor
子類的實例化對象的constractor指向了父類的constractor

4.組合繼承的優化2
Object.create()通過創建中間對象作爲橋樑,將父類和子類的隔離,再重新定義子類構造函數的原型對象的constractor
特點:原型對象作爲參數

      function Parent5 () {
          this.name = 'parent5';
          this.play = [1, 2, 3];
      }
      function Child5 () {
          Parent5.call(this);
          this.type = 'child5';
      }
     Child5.prototype = Object.create(Parent5.prototype);
     Child5.prototype.constructor=Child5;
     var s7 = new Child5();
     console.log(s7 instanceof Child5,s7 instanceof Parent5);
     console.log(s7.constructor);

在這裏插入圖片描述
實例化繼承關係還在,從哪裏直接實例化來的已經找到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章