ES6類和繼承的實現原理是什么

這篇文章給大家分享的是有關ES6類和繼承的實現原理是什么的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

員工經過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質的產品。成都創(chuàng)新互聯(lián)堅持“專注、創(chuàng)新、易用”的產品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網站所以易用所以簡單”。公司專注于為企業(yè)提供成都網站建設、網站制作、微信公眾號開發(fā)、電商網站開發(fā),重慶小程序開發(fā)公司,軟件按需定制網站等一站式互聯(lián)網企業(yè)服務。

1、es6 class 使用

javascript使用的是原型式繼承,我們可以通過原型的特性實現類的繼承,
es6為我們提供了像面向對象繼承一樣的語法糖。

class Parent {
  constructor(a){
    this.filed1 = a;
  }
  filed2 = 2;
  func1 = function(){}
}

class Child extends Parent {
    constructor(a,b) {
      super(a);
      this.filed3 = b;
    }
  
  filed4 = 1;
  func2 = function(){}
}

下面我們借助babel來探究es6類和繼承的實現原理。

1.類的實現

轉換前:

class Parent {
  constructor(a){
    this.filed1 = a;
  }
  filed2 = 2;
  func1 = function(){}
}

轉換后:

function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var Parent = function Parent(a) {
  _classCallCheck(this, Parent);

  this.filed2 = 2;

  this.func1 = function () { };

  this.filed1 = a;
};

可見class的底層依然是構造函數:

1.調用_classCallCheck方法判斷當前函數調用前是否有new關鍵字。

構造函數執(zhí)行前有new關鍵字,會在構造函數內部創(chuàng)建一個空對象,將構造函數的proptype指向這個空對象的_proto_,并將this指向這個空對象。如上,_classCallCheck中:this instanceof Parent 返回true。

若構造函數前面沒有new則構造函數的proptype不會不出現在this的原型鏈上,返回false。

2.將class內部的變量和函數賦給this。

3.執(zhí)行constuctor內部的邏輯。

4.return this (構造函數默認在最后我們做了)。

2.繼承實現

轉換前:

class Child extends Parent {
    constructor(a,b) {
      super(a);
      this.filed3 = b;
    }
  
  filed4 = 1;
  func2 = function(){}
}

轉換后:

我們先看Child內部的實現,再看內部調用的函數是怎么實現的:

var Child = function (_Parent) {
  _inherits(Child, _Parent);

  function Child(a, b) {
    _classCallCheck(this, Child);

    var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, a));

    _this.filed4 = 1;

    _this.func2 = function () {};

    _this.filed3 = b;
    return _this;
  }

  return Child;
}(Parent);

1.調用_inherits函數繼承父類的proptype。

_inherits內部實現:

function _inherits(subClass, superClass) {
  if (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  }
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: { value: subClass, enumerable: false, writable: true, configurable: true }
  });
  if (superClass)
    Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

(1) 校驗父構造函數。

(2) 典型的寄生繼承:用父類構造函數的proptype創(chuàng)建一個空對象,并將這個對象指向子類構造函數的proptype。

(3) 將父構造函數指向子構造函數的_proto_(這步是做什么的不太明確,感覺沒什么意義。)

2.用一個閉包保存父類引用,在閉包內部做子類構造邏輯。

3.new檢查。

4.用當前this調用父類構造函數。

var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, a));

這里的Child.__proto__ || Object.getPrototypeOf(Child)實際上是父構造函數(_inherits最后的操作),然后通過call將其調用方改為當前this,并傳遞參數。(這里感覺可以直接用參數傳過來的Parent)

function _possibleConstructorReturn(self, call) {
  if (!self) {
    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  }
  return call && (typeof call === "object" || typeof call === "function") ? call : self;
}

校驗this是否被初始化,super是否調用,并返回父類已經賦值完的this。

5.將行子類class內部的變量和函數賦給this。

6.執(zhí)行子類constuctor內部的邏輯。

可見,es6實際上是為我們提供了一個“組合寄生繼承”的簡單寫法。

3. super

super代表父類構造函數。

super.fun1() 等同于 Parent.fun1()  或 Parent.prototype.fun1()。

super() 等同于Parent.prototype.construtor()

當我們沒有寫子類構造函數時:

var Child = function (_Parent) {
  _inherits(Child, _Parent);

  function Child() {
    _classCallCheck(this, Child);

    return _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).apply(this, arguments));
  }

  return Child;
}(Parent);

可見默認的構造函數中會主動調用父類構造函數,并默認把當前constructor傳遞的參數傳給了父類。

所以當我們聲明了constructor后必須主動調用super(),否則無法調用父構造函數,無法完成繼承。

典型的例子就是Reatc的Component中,我們聲明constructor后必須調用super(props),因為父類要在構造函數中對props做一些初始化操作。

感謝各位的閱讀!關于ES6類和繼承的實現原理是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享題目:ES6類和繼承的實現原理是什么
分享鏈接:http://muchs.cn/article18/ihgdgp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、搜索引擎優(yōu)化自適應網站、網站設計網頁設計公司、電子商務

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司