這篇文章給大家分享的是有關(guān)ES6類和繼承的實(shí)現(xiàn)原理是什么的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團(tuán)隊(duì)的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。成都創(chuàng)新互聯(lián)堅(jiān)持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因?yàn)椤皩W⑺詫I(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站制作、微信公眾號(hào)開發(fā)、電商網(wǎng)站開發(fā),重慶小程序開發(fā)公司,軟件按需定制網(wǎng)站等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。
1、es6 class 使用
javascript使用的是原型式繼承,我們可以通過原型的特性實(shí)現(xiàn)類的繼承,
es6為我們提供了像面向?qū)ο罄^承一樣的語法糖。
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類和繼承的實(shí)現(xiàn)原理。
1.類的實(shí)現(xiàn)
轉(zhuǎn)換前:
class Parent { constructor(a){ this.filed1 = a; } filed2 = 2; func1 = function(){} }
轉(zhuǎn)換后:
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的底層依然是構(gòu)造函數(shù):
1.調(diào)用_classCallCheck方法判斷當(dāng)前函數(shù)調(diào)用前是否有new關(guān)鍵字。
構(gòu)造函數(shù)執(zhí)行前有new關(guān)鍵字,會(huì)在構(gòu)造函數(shù)內(nèi)部創(chuàng)建一個(gè)空對(duì)象,將構(gòu)造函數(shù)的proptype指向這個(gè)空對(duì)象的_proto_,并將this指向這個(gè)空對(duì)象。如上,_classCallCheck中:this instanceof Parent 返回true。
若構(gòu)造函數(shù)前面沒有new則構(gòu)造函數(shù)的proptype不會(huì)不出現(xiàn)在this的原型鏈上,返回false。
2.將class內(nèi)部的變量和函數(shù)賦給this。
3.執(zhí)行constuctor內(nèi)部的邏輯。
4.return this (構(gòu)造函數(shù)默認(rèn)在最后我們做了)。
轉(zhuǎn)換前:
class Child extends Parent { constructor(a,b) { super(a); this.filed3 = b; } filed4 = 1; func2 = function(){} }
轉(zhuǎn)換后:
我們先看Child內(nèi)部的實(shí)現(xiàn),再看內(nèi)部調(diào)用的函數(shù)是怎么實(shí)現(xiàn)的:
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.調(diào)用_inherits函數(shù)繼承父類的proptype。
_inherits內(nèi)部實(shí)現(xiàn):
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) 校驗(yàn)父構(gòu)造函數(shù)。
(2) 典型的寄生繼承:用父類構(gòu)造函數(shù)的proptype創(chuàng)建一個(gè)空對(duì)象,并將這個(gè)對(duì)象指向子類構(gòu)造函數(shù)的proptype。
(3) 將父構(gòu)造函數(shù)指向子構(gòu)造函數(shù)的_proto_(這步是做什么的不太明確,感覺沒什么意義。)
2.用一個(gè)閉包保存父類引用,在閉包內(nèi)部做子類構(gòu)造邏輯。
3.new檢查。
4.用當(dāng)前this調(diào)用父類構(gòu)造函數(shù)。
var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, a));
這里的Child.__proto__ || Object.getPrototypeOf(Child)實(shí)際上是父構(gòu)造函數(shù)(_inherits最后的操作),然后通過call將其調(diào)用方改為當(dāng)前this,并傳遞參數(shù)。(這里感覺可以直接用參數(shù)傳過來的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; }
校驗(yàn)this是否被初始化,super是否調(diào)用,并返回父類已經(jīng)賦值完的this。
5.將行子類class內(nèi)部的變量和函數(shù)賦給this。
6.執(zhí)行子類constuctor內(nèi)部的邏輯。
可見,es6實(shí)際上是為我們提供了一個(gè)“組合寄生繼承”的簡單寫法。
super代表父類構(gòu)造函數(shù)。
super.fun1() 等同于 Parent.fun1() 或 Parent.prototype.fun1()。
super() 等同于Parent.prototype.construtor()
當(dāng)我們沒有寫子類構(gòu)造函數(shù)時(shí):
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);
可見默認(rèn)的構(gòu)造函數(shù)中會(huì)主動(dòng)調(diào)用父類構(gòu)造函數(shù),并默認(rèn)把當(dāng)前constructor傳遞的參數(shù)傳給了父類。
所以當(dāng)我們聲明了constructor后必須主動(dòng)調(diào)用super(),否則無法調(diào)用父構(gòu)造函數(shù),無法完成繼承。
典型的例子就是Reatc的Component中,我們聲明constructor后必須調(diào)用super(props),因?yàn)楦割愐跇?gòu)造函數(shù)中對(duì)props做一些初始化操作。
感謝各位的閱讀!關(guān)于ES6類和繼承的實(shí)現(xiàn)原理是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享題目:ES6類和繼承的實(shí)現(xiàn)原理是什么
分享鏈接:http://chinadenli.net/article18/ihgdgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)公司、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)