首頁 > web前端 > js教程 > 主體

ES6類別和繼承的實作原理(程式碼範例)

不言
發布: 2019-01-10 10:47:40
轉載
3509 人瀏覽過

這篇文章帶給大家的內容是關於ES6類別和繼承的實現原理(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

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關鍵字。

建構子執行前有new關鍵字,會在建構子內部建立一個空對象,將建構子的proptype指向這個空對象的_proto_,並將this指向這個空對象。如上,_classCallCheck中:this instanceof Parent 傳回true。

若建構子前面沒有new則建構子的proptype不會不出現在this的原型鏈上,回傳false。

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

3.執行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建立一個空對象,並將這個物件指向子類別建構子的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.執行子類別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類別和繼承的實作原理(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:segmentfault.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!