在閱讀文章以前,您至少須要對JavaScript原型繼承有必定了解,若是以爲有所欠缺,能夠先了解下我這篇文章: https://segmentfault.com/a/11...
javascript使用的是原型式繼承,咱們能夠經過原型的特性實現類的繼承,
es6爲咱們提供了像面向對象繼承同樣的語法糖。javascript
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類和繼承的實現原理。java
轉換前:es6
class Parent { constructor(a){ this.filed1 = a; } filed2 = 2; func1 = function(){} }
轉換後:express
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的底層依然是構造函數:segmentfault
1.調用_classCallCheck方法判斷當前函數調用前是否有new關鍵字。babel
構造函數執行前有new關鍵字,會在構造函數內部建立一個空對象,將構造函數的proptype指向這個空對象的_proto_,並將this指向這個空對象。如上,_classCallCheck中:this instanceof Parent 返回true。若構造函數前面沒有new則構造函數的proptype不會不出如今this的原型鏈上,返回false。閉包
2.將class內部的變量和函數賦給this。app
3.執行constuctor內部的邏輯。函數
4.return this (構造函數默認在最後咱們作了)。this
轉換前:
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其實是爲咱們提供了一個「組合寄生繼承」的簡單寫法。
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作一些初始化操做。