詳解ES6中class的實現(xiàn)原理
一、在ES6以前實現(xiàn)類和繼承
實現(xiàn)類的代碼如下:
function Person(name, age) { this.name = name; this.age = age; } Person.prototype.speakSomething = function () { console.log("I can speek chinese"); };
實現(xiàn)繼承的代碼如下:一般使用原型鏈繼承和call繼承混合的形式
function Person(name) { this.name = name; } Person.prototype.showName = function () { return `名字是:${this.name}`; }; function Student(name, skill) { Person.call(this, name);//繼承屬性 this.skill = skill; } Student.prototype = new Person();//繼承方法
二、ES6使用class定義類
class Parent { constructor(name,age){ this.name = name; this.age = age; } speakSomething(){ console.log("I can speek chinese"); } }
經(jīng)過babel轉(zhuǎn)碼之后
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Parent = function () { function Parent(name, age) { _classCallCheck(this, Parent); this.name = name; this.age = age; } _createClass(Parent, [{ key: "speakSomething", value: function speakSomething() { console.log("I can speek chinese"); } }]); return Parent; }();
可以看到ES6類的底層還是通過構(gòu)造函數(shù)去創(chuàng)建的。
通過ES6創(chuàng)建的類,是不允許你直接調(diào)用的。在ES5中,構(gòu)造函數(shù)是可以直接運行的,比如Parent()。但是在ES6就不行。我們可以看到轉(zhuǎn)碼的構(gòu)造函數(shù)中有_classCallCheck(this, Parent)語句,這句話是防止你通過構(gòu)造函數(shù)直接運行的。你直接在ES6運行Parent(),這是不允許的,ES6中拋出Class constructor Parent cannot be invoked without 'new'錯誤。轉(zhuǎn)碼后的會拋出Cannot call a class as a function.能夠規(guī)范化類的使用方式。
轉(zhuǎn)碼中_createClass方法,它調(diào)用Object.defineProperty方法去給新創(chuàng)建的Parent添加各種屬性。defineProperties(Constructor.prototype, protoProps)是給原型添加屬性。如果你有靜態(tài)屬性,會直接添加到構(gòu)造函數(shù)defineProperties(Constructor, staticProps)上。
三、ES6實現(xiàn)繼承
我們給Parent添加靜態(tài)屬性,原型屬性,內(nèi)部屬性。
class Parent { static height = 12 constructor(name,age){ this.name = name; this.age = age; } speakSomething(){ console.log("I can speek chinese"); } } Parent.prototype.color = 'yellow' //定義子類,繼承父類 class Child extends Parent { static width = 18 constructor(name,age){ super(name,age); } coding(){ console.log("I can code JS"); } }
經(jīng)過babel轉(zhuǎn)碼之后
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 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; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Parent = function () { function Parent(name, age) { _classCallCheck(this, Parent); this.name = name; this.age = age; } _createClass(Parent, [{ key: "speakSomething", value: function speakSomething() { console.log("I can speek chinese"); } }]); return Parent; }(); Parent.height = 12; Parent.prototype.color = 'yellow'; //定義子類,繼承父類 var Child = function (_Parent) { _inherits(Child, _Parent); function Child(name, age) { _classCallCheck(this, Child); return _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, name, age)); } _createClass(Child, [{ key: "coding", value: function coding() { console.log("I can code JS"); } }]); return Child; }(Parent); Child.width = 18;
構(gòu)造類的方法都沒變,只是添加了_inherits核心方法來實現(xiàn)繼承。具體步驟如下:
首先是判斷父類的類型,然后:
subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });
這段代碼翻譯下來就是
function F(){} F.prototype = superClass.prototype subClass.prototype = new F() subClass.prototype.constructor = subClass
接下來就是subClass.__proto__ = superClass
_inherits核心思想就是下面兩句:
subClass.prototype.__proto__ = superClass.prototype subClass.__proto__ = superClass
如下圖所示:
首先 subClass.prototype.__proto__ = superClass.prototype保證了子類的實例instanceof父類是true,子類的實例可以訪問到父類的屬性,包括內(nèi)部屬性,以及原型屬性。
其次,subClass.__proto__ = superClass,保證了靜態(tài)屬性也能訪問到,也就是這個例子中的Child.height。
以上就是詳解ES6中class的實現(xiàn)原理的詳細(xì)內(nèi)容,更多關(guān)于ES6中class的實現(xiàn)原理的資料請關(guān)注腳本之家其它相關(guān)文章!
- 原生JavaScript之es6中Class的用法分析
- ES6 class類鏈?zhǔn)嚼^承,實例化及react super(props)原理詳解
- ES6 Class中實現(xiàn)私有屬性的一些方法總結(jié)
- ES6 class的應(yīng)用實例分析
- ES6中的class是如何實現(xiàn)的(附Babel編譯的ES5代碼詳解)
- es6新特性之 class 基本用法解析
- 用ES6的class模仿Vue寫一個雙向綁定的示例代碼
- 解讀ES6中class關(guān)鍵字
- ES6 javascript中Class類繼承用法實例詳解
- ES6 javascript中class靜態(tài)方法、屬性與實例屬性用法示例
相關(guān)文章
javascript中節(jié)點的最近的相關(guān)節(jié)點訪問方法
parentNode——父節(jié)點;firstChild——第一個子節(jié)點;lastChild——最后一個子節(jié)點;previousSibling——緊挨著的前面的兄弟節(jié)點;這樣就可以作短途旅行,訪問當(dāng)前節(jié)點的某些相關(guān)節(jié)點,感興趣的你可以參考下哈2013-03-03JavaScript提高網(wǎng)站性能優(yōu)化的建議(二)
這篇文章主要介紹了JavaScript提高網(wǎng)站性能優(yōu)化的建議(二)的相關(guān)資料,需要的朋友可以參考下2016-07-07JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)
這篇文章主要介紹了JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04JavaScript事件循環(huán)同步任務(wù)與異步任務(wù)
這篇文章主要介紹了JavaScript事件循環(huán)同步任務(wù)與異步任務(wù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07javascript實現(xiàn)當(dāng)前頁導(dǎo)航激活的方法
這篇文章主要介紹了javascript實現(xiàn)當(dāng)前頁導(dǎo)航激活的方法,涉及javascript操作css的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02詳解JS中統(tǒng)計函數(shù)執(zhí)行次數(shù)與執(zhí)行時間
這篇文章給大家分享了JS中統(tǒng)計函數(shù)執(zhí)行次數(shù)與執(zhí)行時間的相關(guān)知識點內(nèi)容,有興趣的朋友們分享下。2018-09-09