理解 es6 中class構造以及繼承的底層實現原理
原文鏈接:https://blog.csdn.net/qq_34149805/article/details/86105123
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類和繼承的實現原理
2、類的實現
轉換前:
class Parent { constructor(a){ this.filed1 = a; } filed2 = 2; func1 = function(){} }
轉換后:
function _classCallCheck(instance, Constructor) {
// instanceof 檢測構造函數的prototype
屬性是否出現在某個實例對象的原型鏈上。 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關鍵字,會在構造函數內部創建一個空對象,將構造函數的prototype指向這個空對象的__prpto__,並將this指向這個空對象。如上,_classCallCheck中:this instanceof Parent,返回true。
若構造函數前面沒有new則構造函數的prototype不會出現在this的原型鏈上,返回false
2)將class內部的變量函數賦值給this
3)執行constructor內部的邏輯
4)return this(構造函數默認在最后幫我們做了這一步)
3、繼承實現
轉換前:
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);
① 調用_inherits函數繼承父類的prototype
_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. 典型的寄生繼承:用父類構造函數的prototype創建一個空對象,並將這個對象指向子類構造函數的prototype
3. 將父構造函數指向子構造函數的_proto_
② 用一個閉包保存父類引用,在閉包內部做子類構造邏輯
③ new 檢查
④ 用當前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。
⑤ 執行子類class內部的變量和函數賦給this
⑥ 執行子類constructor內部邏輯
可見,ES6實際上是提供了一個"組合寄生繼承"的簡單寫法
4、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做一些初始化操作。